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