ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

smoothscroll.js (10702B)


      1 var currentPageURL = document.location.toString();
      2 var isAnchor = false;
      3 if (location.hash) {
      4     isAnchor = true;
      5     window.scrollTo(0, 0);
      6 }
      7 (function ($) {
      8     if (window.useManagedSmoothScroll) {
      9         return;
     10     }
     11 
     12     var duration = 1500;
     13     var easing = 'easeInOutQuart';
     14     var lastId, anchors, scrollItems;
     15 
     16     function targetIsSamePage(target) {
     17         return !target || target == "_self";
     18     }
     19 
     20     function getHash(url) {
     21         if (!url) {
     22             return false;
     23         }
     24         var indexOfHash = url.indexOf('#');
     25         if (indexOfHash > -1) {
     26             if (indexOfHash === 0) {
     27                 return url.replace('#', '');
     28             }
     29             var hash = url.substring(indexOfHash + 1);
     30             var urlQuery = "";
     31             if (url.indexOf('?') > -1) {
     32                 urlQuery = url.substring(url.indexOf('?'));
     33             }
     34             var absLinkRegExp = /(https?|file):\/\//;
     35             var pageLocation = window.location.pathname;
     36             var urlLocation = url.replace(urlQuery, '').replace('#' + hash, '').replace(absLinkRegExp, '');
     37             if (url.match(absLinkRegExp)) {
     38                 pageLocation = window.location.host + pageLocation;
     39             } else {
     40                 urlLocation = pageLocation.substring(0, pageLocation.lastIndexOf("/")) + "/" + urlLocation;
     41             }
     42             if (pageLocation == urlLocation || pageLocation == urlLocation + "/") {
     43                 return hash;
     44             }
     45         }
     46         return false;
     47     }
     48 
     49     function change_url_hash(hash) {
     50         setTimeout(function () {
     51             if (hash) {
     52                 hash = "#" + hash;
     53             } else {
     54                 hash = "";
     55             }
     56             if (history && history.replaceState) {
     57                 history.replaceState({}, "", hash);
     58             } else {
     59             }
     60         }, 100);
     61         /* safari issue fixed by throtteling the event */
     62     }
     63 
     64     var scrollStarted = false;
     65 
     66 
     67     function scrollToSection(section, elem) {
     68         if (scrollStarted) {
     69             return;
     70         }
     71 
     72         try {
     73             scrollStarted = true;
     74 
     75             if (section) {
     76                 var parent;
     77                 if (elem) {
     78                     parent = elem.parent().parentsUntil('body').filter(function () {
     79                         if (jQuery(this).css('position') == "fixed" && !jQuery(this).is('.mobile-overlay')) return jQuery(this);
     80                     }).eq(0);
     81                 }
     82 
     83                 var topDistance = 0;
     84                 if (parent && parent.length) {
     85                     var parentClass = parent.attr("class");
     86                     var flexiMenu = jQuery('div[class*="main-menu"]');
     87                     if (parent.outerHeight() !== window.innerHeight || !parent.is('.full-sidebar')) {
     88                         topDistance = parent.outerHeight() + parent.position().top;
     89                     }
     90                 }
     91                 var scrollToValue = section.offset().top - topDistance;
     92                 if (scrollToValue < 0) {
     93                     scrollToValue = 0;
     94                 }
     95                 var stickTo = jQuery("[data-cp-shrink=initial]");
     96                 if (scrollToValue > stickTo.height()) {
     97                     scrollToValue -= jQuery('[data-cp-shrink=shrinked]').height();
     98                 }
     99 
    100                 var ratio = Math.max(0.5, scrollToValue / jQuery('body').height());
    101 
    102                 jQuery('html, body').animate({
    103                     scrollTop: scrollToValue
    104                 }, duration * ratio, easing, function () {
    105                     scrollStarted = false;
    106                     jQuery(window).trigger('scroll');
    107                     jQuery(document).trigger('scroll');
    108                 });
    109 
    110                 return true;
    111             }
    112         } catch (e) {
    113             // alert('error in xtd one page site script ' + e);
    114         }
    115     }
    116 
    117     function linkClick(ev, elem) {
    118 
    119         if (!targetIsSamePage(elem.attr("target"))) {
    120             return;
    121         }
    122 
    123         var section = elem.data('onepage-section') ? elem.data('onepage-section') : false;
    124 
    125         if (section && section.length) {
    126             ev.preventDefault();
    127 
    128             // ev.stopPropagation();
    129         }
    130 
    131         var scrolled = scrollToSection(section, elem);
    132         if (scrolled && ev) {
    133             ev.preventDefault();
    134         }
    135 
    136     }
    137 
    138     function bubbleSortByTop(arr) {
    139         var swapped;
    140         do {
    141             swapped = false;
    142             for (var i = 0; i < arr.length - 1; i++) {
    143                 var elem = arr[i];
    144                 var elem2 = arr[i + 1];
    145                 if (elem.offset().top > elem2.offset().top) {
    146                     var temp = arr[i];
    147                     arr[i] = arr[i + 1];
    148                     arr[i + 1] = temp;
    149                     swapped = true;
    150                 }
    151             }
    152         } while (swapped);
    153     }
    154 
    155     function getAnchors() {
    156         scrollItems = [];
    157         anchors = jQuery('a:not(.screen-reader-text)').filter(function () {
    158             var elem = jQuery(this);
    159             var href = elem.attr('href');
    160             var target = elem.attr('target');
    161             var hash = getHash(href);
    162             if (hash && hash !== 'wp-toolbar') {
    163                 try {
    164                     var section = jQuery("#" + hash);
    165                     if (section.length > 0) {
    166                         elem.data('onepage-section', section);
    167                         if (elem.parent()[0].tagName == "LI") {
    168                             var dataElem = section.data('onepage-anchor') || $("");
    169                             dataElem = dataElem.add(elem);
    170                             section.data('onepage-anchor', dataElem);
    171                         }
    172                         scrollItems.push(section);
    173                         return true;
    174                     }
    175                 } catch (e) {
    176 
    177                 }
    178             }
    179             return false;
    180         });
    181 
    182 
    183         anchors.each(function () {
    184 
    185             if (jQuery(this).closest('.fm2_mobile_jq_menu').length || !jQuery(this).is(':visible')) {
    186                 return;
    187             }
    188 
    189             if (jQuery(this).parent().is('li.menu-item')) {
    190                 var selfAnchor = this;
    191                 jQuery(this).unbind('click.onepage');
    192                 jQuery(this).attr('data-smoothscroll', 'true');
    193                 jQuery(this).parent().unbind('click.onepage').bind("click.onepage", function (e) {
    194 
    195                     if (!jQuery(e.target).parent().is(e.currentTarget)) {
    196                         return;
    197                     }
    198 
    199                     e.preventDefault();
    200                     e.stopPropagation();
    201                     linkClick(e, jQuery(selfAnchor));
    202                 });
    203             } else {
    204                 jQuery(this).unbind('click.onepage').bind("click.onepage", function (e) {
    205 
    206                     linkClick(e, jQuery(this));
    207                 });
    208             }
    209         });
    210         try {
    211             bubbleSortByTop(scrollItems);
    212         } catch (e) {
    213         }
    214     }
    215 
    216     var scrollTimeout;
    217     var is_touch_device = 'ontouchstart' in document.documentElement;
    218     if (!is_touch_device) {
    219         jQuery(window).scroll(function () {
    220             clearTimeout(scrollTimeout);
    221             scrollTimeout = setTimeout(doneScrolling, 20);
    222         });
    223     }
    224 
    225     function doneScrolling() {
    226         var windowElem = jQuery(window);
    227         var fromTop = windowElem.scrollTop() + window.innerHeight * 0.5;
    228         var cur = [];
    229         if (!scrollItems) {
    230             getAnchors();
    231         }
    232         for (var i = 0; i < scrollItems.length; i++) {
    233             if (scrollItems[i].offset().top < fromTop) {
    234                 cur.push(scrollItems[i]);
    235             }
    236         }
    237         var lastItem = scrollItems[scrollItems.length - 1];
    238         if ((windowElem.scrollTop() + windowElem.height() + 50) >= jQuery(document).height()) {
    239             cur.push(lastItem);
    240         }
    241         cur = cur[cur.length - 1];
    242         var id = cur && cur.length ? cur[0].id : "";
    243         change_url_hash(id);
    244         if (id.length === 0 && anchors) {
    245             // anchors.closest('ul').find('.current_page_item').removeClass('current_page_item');
    246             anchors.parent().addBack().removeClass('current_page_item current-menu-item');
    247             var loc = (window.location + "").split('#')[0].replace(/\/$/, "");
    248             anchors.closest('ul').find('[href$="' + loc + '"]').parent().addBack().addClass('current-menu-item');
    249             if (!loc.length) {
    250                 anchors.closest('ul').find('[href$="' + window.location + '"]').parent().addBack().addClass('current-menu-item');
    251 
    252             }
    253         }
    254 
    255         if (lastId !== id && id.length) {
    256             lastId = id;
    257             try {
    258                 anchors.filter('.current_page_item, .current-menu-item').each(function () {
    259                     jQuery(this).parent().addBack().removeClass('current_page_item current-menu-item');
    260 
    261                 });
    262                 anchors.closest('ul').find('.current_page_item, .current-menu-item').removeClass('current_page_item current-menu-item');
    263                 cur.data('onepage-anchor').each(function () {
    264                     $(this).parent().addBack().addClass('current-menu-item');
    265                 });
    266             } catch (e) {
    267             }
    268         }
    269     }
    270 
    271     var id;
    272     jQuery(window).bind("resize orientationchange", function () {
    273         clearTimeout(id);
    274         id = setTimeout(doneResizing, 100);
    275     });
    276 
    277     function doneResizing() {
    278         getAnchors();
    279     }
    280 
    281     getAnchors();
    282 
    283     is_touch_device = 'ontouchstart' in document.documentElement;
    284 
    285     if (!is_touch_device) {
    286         doneScrolling();
    287     }
    288     if (isAnchor) {
    289         if (jQuery.find('a[href^="' + currentPageURL + '"]').length > 0) {
    290             jQuery(jQuery.find('a[href="' + currentPageURL + '"]')).trigger('click');
    291         } else {
    292             var hash = getHash(currentPageURL);
    293             if (hash.length) {
    294                 jQuery(jQuery.find('a[href*="#' + hash + '"]')).trigger('click');
    295             }
    296         }
    297     } else {
    298         jQuery('a[href*="#"]').each(function (index, el) {
    299             var parts = el.href.split('#'),
    300                 anchor = parts[parts.length - 1];
    301 
    302             if (parts.length >= 2) {
    303                 if (anchor.length) {
    304                     jQuery(this).parent().addBack().removeClass('current_page_item current-menu-item');
    305                 }
    306             }
    307         });
    308     }
    309 
    310     if (window.wp && window.wp.customize) {
    311         $('.dropdown-menu').parent('div').parent().on('DOMNodeInserted DOMNodeRemoved', function (event) {
    312             getAnchors();
    313             doneScrolling();
    314         });
    315     }
    316 
    317     window.scrollToSection = scrollToSection;
    318     window.smoothScrollGetAnchors = getAnchors;
    319 })(jQuery);
    320 
    321