Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. jQuery(document).ready(function (event) {
  2.  
  3. // get rooturl via localize script
  4. var rootUrl = aws_data.rootUrl;
  5. var isAnimating = false,
  6. newLocation = '',
  7. firstLoad = false;
  8.  
  9. // Internal Helper
  10. $.expr[':'].internal = function(obj, index, meta, stack){
  11. // Prepare
  12. var
  13. $this = $(obj),
  14. urlinternal = $this.attr('href')||'',
  15. isInternalLink;
  16.  
  17. // Check link
  18. isInternalLink = urlinternal.substring(0,rootUrl.length) === rootUrl || urlinternal.indexOf(':') === -1;
  19.  
  20. // Ignore or Keep
  21. return isInternalLink;
  22. };
  23.  
  24.  
  25.  
  26. //trigger smooth transition from the actual page to the new one on relevant links
  27. $('main').on('click', 'a[href]:internal:not(.no-ajaxy,.love-button,[href^="#"],[href="#"],[href*="#respond"],[href*="wp-login"],[href*="wp-admin"])', function (event) {
  28. event.preventDefault();
  29. //detect which page has been selected
  30. var newPage = $(this).attr('href');
  31. //if the page is not already being animated - trigger animation
  32. if (!isAnimating) changePage(newPage, true);
  33. firstLoad = true;
  34. });
  35. //detect the 'popstate' event - e.g. user clicking the back button
  36. $(window).on('popstate', function (e) {
  37. if (firstLoad) {
  38. /*
  39. Safari emits a popstate event on page load - check if firstLoad is true before animating
  40. if it's false - the page has just been loaded
  41. */
  42. var newPageArray = location.pathname.split('/'), //this is the url of the page to be loaded
  43. //newPage = newPageArray[newPageArray.length - 1];
  44. newPage = window.location.href;
  45. if (!isAnimating && newLocation != newPage) changePage(newPage, false);
  46. }
  47. firstLoad = true;
  48. });
  49.  
  50. function changePage(url, bool) {
  51. isAnimating = true;
  52. // trigger page animation
  53. $('body').addClass('page-is-changing');
  54. $('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
  55. loadNewContent(url, bool);
  56. newLocation = url;
  57. $('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
  58. });
  59. //if browser doesn't support CSS transitions
  60. if (!transitionsSupported()) {
  61. loadNewContent(url, bool);
  62. newLocation = url;
  63. }
  64. }
  65.  
  66. function loadNewContent(url, bool) {
  67.  
  68. url = ('' === url) ? rootUrl : url;
  69. //var newSection = 'cd-' + url.replace(rootUrl, "");
  70. var section = $('<div class="cd-main-content"></div>');
  71.  
  72. $.ajax({url: url,
  73. success: function(data){
  74. data = data.replace("<body", "<container").replace("body>", "container>");
  75. var classes = $(data).filter("container").attr("class");
  76. $("body").attr("class", classes + " page-is-changing");
  77. }
  78. });
  79.  
  80. section.load(url + ' .cd-main-content > *', function (response, status, xhr) {
  81.  
  82.  
  83. // load new content and replace <main> content with the new one
  84. $('main').html(section);
  85. //if browser doesn't support CSS transitions - dont wait for the end of transitions
  86. var delay = 1200;
  87.  
  88. //function to execute after dom is loaded
  89. $(document).foundation();
  90. makeFooterSticky();
  91. window.scrollTo(0, 0);
  92. $(".ajax-load-more-wrap").ajaxloadmore();
  93. //ga('send', 'pageview', window.location.pathname);
  94.  
  95. setTimeout(function () {
  96. //wait for the end of the transition on the loading bar before revealing the new content
  97. $('body').removeClass('page-is-changing');
  98. $('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
  99. isAnimating = false;
  100. $('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
  101. });
  102. if (!transitionsSupported()) isAnimating = false;
  103. }, delay);
  104. if (url != window.location && bool) {
  105. //add the new page to the window.history
  106. //if the new page was triggered by a 'popstate' event, don't add it
  107. window.history.pushState({
  108. path: url
  109. }, '', url);
  110. }
  111. });
  112.  
  113. }
  114.  
  115. function transitionsSupported() {
  116. return $('html').hasClass('csstransitions');
  117. }
  118. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement