forked from d4nyll/smartscroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartscroll.min.js
More file actions
1 lines (1 loc) · 4.82 KB
/
smartscroll.min.js
File metadata and controls
1 lines (1 loc) · 4.82 KB
1
!function(b){var y,k="mousewheel DOMMouseScroll wheel MozMousePixelScroll";"undefined"!=typeof Lethargy&&null!==Lethargy&&(y=new Lethargy);var H=function(){return Math.max(window.pageYOffset,window.document.body.scrollTop,window.document.documentElement.scrollTop)};b.smartscroll=function(e){var a=b.extend({},b.smartscroll.defaults,e);if(a.sectionSelector||(a.sectionSelector="."+a.sectionClass),("undefined"==typeof EventEmitter||null===EventEmitter||a.eventEmitter&&a.eventEmitter.constructor!==EventEmitter)&&(a.eventEmitter=null),a.bindSwipe)var l=null,s=null,t=function(e){var t=e.originalEvent||e;l=t.touches[0].clientX,s=t.touches[0].clientY},n=function(e){var t=e.originalEvent||e;if(l&&s){var n=t.touches[0].clientX,i=t.touches[0].clientY,o=l-n,r=s-i;Math.abs(o)>Math.abs(r)?0<o?a.eventEmitter.emitEvent("swipeLeft"):a.eventEmitter.emitEvent("swipeRight"):0<r?a.eventEmitter.emitEvent("swipeUp"):a.eventEmitter.emitEvent("swipeDown"),s=l=null}};var c,d,h=!1,u=[],i=!1,o=!1,r=window.location.hash,w=b(a.sectionWrapperSelector+":first"),v=function(){var e=H(),t=e+b(window).height();return t!==b(document).height()&&(c<t&&e<=d)},p=function(){var n=[];c=Math.round(w.position().top+parseInt(w.css("paddingTop"),10)+parseInt(w.css("borderTopWidth"),10)+parseInt(w.css("marginTop"),10)),d=Math.round(c+w.height(),10),n.push(c),b(a.sectionSelector).each(function(e,t){n.push(Math.round(c+b(t).position().top+b(t).outerHeight()))}),u=n},f=function(e){for(var t=0;t<u.length;t+=1)if(e<=u[t])return t;return u.length},m=function(){var e;if(H()+b(window).height()/2<c)e=a.headerHash;else{var t=f(H()+b(window).height()/2);t!==undefined&&(e=b(a.sectionSelector+":nth-of-type("+t+")").data("hash"))}void 0!==e&&window.location.hash==="#"+e||(void 0===e&&(e=a.headerHash),a.keepHistory?window.location.hash=e:window.location.replace(window.location.href.split("#")[0]+"#"+e))},E=function(e,t){h||(h=!0,b("body,html").stop(!0,!0).animate({scrollTop:e},t,function(){if(h=!1,a.eventEmitter){var e=H(),t=f(e+b(window).height()/2);a.eventEmitter.emitEvent("scrollEnd",[t])}}))};this.scroll=function(e){if(u){var t,n=H();if(a.eventEmitter){t=f(n+b(window).height()/2);var i=e?t+1:t-1;a.eventEmitter.emitEvent("scrollStart",[i])}for(var o=0;o<u.length;o+=1)if(n<u[o])return E(e?u[o]:u[o-1]-b(window).height(),700),a.eventEmitter&&a.eventEmitter.emitEvent("scrollEnd",[t]),!1}return undefined};var g=function(){b(window).bind(k,function(e){var t=function(e){var t;if(y&&(t=y.check(e)),!h)if(y){if(1===t)return"up";if(-1===t)return"down"}else{if(0<e.originalEvent.wheelDelta||e.originalEvent.detail<0)return"up";if(e.originalEvent.wheelDelta<0||0<e.originalEvent.detail)return"down"}return!1}(e);a.dynamicHeight&&p();var n=H(),i=n+b(window).height();if(c<i&&n<=d&&v()){var o=f(n),r=f(n+b(window).height()/2);o===f(i)&&a.innerSectionScroll||(e.preventDefault(),e.stopPropagation(),t&&("up"===t?(a.toptotop?E(u[r-2]+1,a.animationSpeed):E(u[r-1]-b(window).height(),a.animationSpeed),a.eventEmitter&&a.eventEmitter.emitEvent("scrollStart",[r-1])):"down"===t&&(E(u[r]+1,a.animationSpeed),a.eventEmitter&&a.eventEmitter.emitEvent("scrollStart",[r+1]))))}})};if(w.css({position:"relative"}),setTimeout(function(){if(p(),a.autoHash&&(null===a.eventEmitter||a.hashContinuousUpdate?b(window).bind("scroll",m):a.eventEmitter.addListener("scrollEnd",m)),a.initialScroll&&0<r.length){var e=b('[data-hash="'+r.substr(1)+'"]');0<e.length&&E(e[0].offsetTop+c,0)}},50),b(window).bind("resize",p),null!==a.breakpoint&&a.breakpoint===parseInt(a.breakpoint,10)&&0<a.breakpoint&&(i=!0),"vp"===a.mode)if(a.ie8){var S=function(){b(a.sectionSelector).css({height:b(window).height()})};S(),b(window).bind("resize",S)}else b(a.sectionSelector).css({height:"100vh"});if(a.sectionScroll&&(i&&b(window).bind("resize",function(){if(b(window).width()<a.breakpoint){if(!o)return b(window).unbind(k),!(o=!0)}else o&&(g(),o=!1);return undefined}),g()),a.bindSwipe&&(b(window).on("touchstart",t),b(window).on("touchmove",n)),a.bindKeyboard){b(window).on("keydown",function(e){var t=e.originalEvent||e;a.dynamicHeight&&p();var n=H(),i=n+b(window).height();if(v()){var o=f(n),r=f(n+b(window).height()/2);if(o!==f(i)||!a.innerSectionScroll)switch(t.which){case 38:t.preventDefault(),t.stopPropagation(),a.toptotop?E(u[r-2]+1,a.animationSpeed):E(u[r-1]-b(window).height(),a.animationSpeed),a.eventEmitter&&a.eventEmitter.emitEvent("scrollStart",[r-1]);break;case 40:t.preventDefault(),t.stopPropagation(),E(u[r]+1,a.animationSpeed),a.eventEmitter&&a.eventEmitter.emitEvent("scrollStart",[r+1])}}})}return this},b.smartscroll.defaults={animationSpeed:700,autoHash:!0,breakpoint:null,initialScroll:!0,headerHash:"header",keepHistory:!1,mode:"vp",sectionClass:"section",sectionSelector:null,sectionScroll:!0,sectionWrapperSelector:".section-wrapper",eventEmitter:null,dynamicHeight:!1,ie8:!1,hashContinuousUpdate:!0,innerSectionScroll:!0,toptotop:!1,bindSwipe:!0,bindKeyboard:!0}}(jQuery);