forked from lukehaas/Scrollify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollify.min.js
1 lines (1 loc) · 6.66 KB
/
jquery.scrollify.min.js
1
!function(e,t,n,o){"use strict";function s(n,o){return S===!0?!0:void(f[n]&&(w=!1,H.before(n,p),v=1,H.sectionName&&(history.pushState?history.replaceState(null,null,f[n]):t.location.hash=f[n]),o?(e(H.target).stop().scrollTop(d[n]),H.after(n,p)):(e(H.target).stop().animate({scrollTop:d[n]},H.scrollSpeed,H.easing),e(H.target).promise().done(function(){E=!1,H.after(n,p)}))))}function i(e){if(4>e)return!1;var t,n=20,o=0,s=e.length-1;for(e.length<2*n&&(n=Math.floor(e.length/2)),t=e.length-n;s>=t;s--)o+=e[s];var i=o/n;for(o=0,s=e.length-n-1,t=e.length-2*n;s>=t;s--)o+=e[s];var c=o/n;return i>=c?!0:!1}function c(e,t){for(var n=f.length;n>=0;n--)"string"==typeof e?f[n]===e&&(g=n,s(n,t)):n===e&&(g=n,s(n,t))}var r,u,a,h,l,d=[],f=[],p=[],m=[],g=0,v=1,y=!1,b=e(t).scrollTop(),w=!1,E=!1,M=!1,S=!1,T=[],x=(new Date).getTime(),H={section:"section",sectionName:"section-name",easing:"easeOutExpo",scrollSpeed:1100,offset:0,scrollbars:!0,axis:"y",target:"html,body",standardScrollElements:!1,before:function(){},after:function(){},afterResize:function(){},afterRender:function(){}};e.scrollify=function(o){function c(t){e(H.target).stop().animate({scrollTop:t},H.scrollSpeed,H.easing)}function D(){e(H.section).each(function(n){e(this).css("height","auto").outerHeight()<e(t).height()?(e(this).css({height:e(t).height()}),m[n]=!1):(e(this).css({height:e(this).height()}),m[n]=!0)})}function L(n){e(H.section).each(function(n){n>0?d[n]=parseInt(e(this).offset().top)+H.offset:d[n]=parseInt(e(this).offset().top),H.sectionName&&e(this).data(H.sectionName)?f[n]="#"+e(this).data(H.sectionName).replace(/ /g,"-"):f[n]="#"+(n+1),p[n]=e(this),t.location.hash===f[n]&&(g=n,y=!0)}),!0===n?s(g,!1):H.afterRender()}function z(){return b=e(t).scrollTop(),b>parseInt(d[g])?!1:!0}function N(){return b=e(t).scrollTop(),b<parseInt(d[g])+(p[g].height()-e(t).height())?!1:!0}e.easing.easeOutExpo=function(e,t,n,o,s){return t==s?n+o:o*(-Math.pow(2,-10*t/s)+1)+n},a={handleMousedown:function(){return S===!0?!0:(w=!1,void(M=!1))},handleMouseup:function(){return S===!0?!0:(w=!0,void(M&&a.calculateNearest()))},handleScroll:function(){return S===!0?!0:(r&&clearTimeout(r),void(r=setTimeout(function(){return M=!0,w===!1?!1:(w=!1,void a.calculateNearest())},200)))},calculateNearest:function(){b=e(t).scrollTop();for(var n,o=1,i=d.length,c=0,r=Math.abs(d[0]-b);i>o;o++)n=Math.abs(d[o]-b),r>n&&(r=n,c=o);(N()||z())&&(g=c,s(c,!1))},wheelHandler:function(t,n){if(S===!0)return!0;if(H.standardScrollElements&&(e(t.target).is(H.standardScrollElements)||e(t.target).closest(H.standardScrollElements).length))return!0;m[g]||t.preventDefault();var o=(new Date).getTime();if(n=n||-t.originalEvent.detail/3||t.originalEvent.wheelDelta/120,o-x>1300&&(T=[]),x=o,T.length>=35&&T.shift(),T.push(Math.abs(10*n)),E)return!1;if(0>n){if(g<d.length-1&&N()){if(!i(T))return!1;t.preventDefault(),g++,E=!0,s(g,!1)}}else if(n>0&&g>0&&z()){if(!i(T))return!1;t.preventDefault(),g--,E=!0,s(g,!1)}},keyHandler:function(e){return S===!0?!0:void(38==e.keyCode?g>0&&z()&&(g--,s(g,!1)):40==e.keyCode&&g<d.length-1&&N()&&(g++,s(g,!1)))},init:function(){H.scrollbars?(e(t).bind("mousedown",a.handleMousedown),e(t).bind("mouseup",a.handleMouseup),e(t).bind("scroll",a.handleScroll)):e("body").css({overflow:"hidden"}),e(n).bind("DOMMouseScroll mousewheel",a.wheelHandler),e(n).bind("keydown",a.keyHandler)}},h={touches:{touchstart:{y:-1,x:-1},touchmove:{y:-1,x:-1},touchend:!1,direction:"undetermined"},options:{distance:30,timeGap:800,timeStamp:(new Date).getTime()},touchHandler:function(t){if(S===!0)return!0;if(H.standardScrollElements&&(e(t.target).is(H.standardScrollElements)||e(t.target).closest(H.standardScrollElements).length))return!0;var n;if("undefined"!=typeof t&&"undefined"!=typeof t.touches)switch(n=t.touches[0],t.type){case"touchstart":h.touches.touchstart.y=n.pageY,h.touches.touchmove.y=-1,h.touches.touchstart.x=n.pageX,h.touches.touchmove.x=-1,h.options.timeStamp=(new Date).getTime(),h.touches.touchend=!1;case"touchmove":h.touches.touchmove.y=n.pageY,h.touches.touchmove.x=n.pageX,h.touches.touchstart.y!==h.touches.touchmove.y&&Math.abs(h.touches.touchstart.y-h.touches.touchmove.y)>Math.abs(h.touches.touchstart.x-h.touches.touchmove.x)&&(t.preventDefault(),h.touches.direction="y",h.options.timeStamp+h.options.timeGap<(new Date).getTime()&&0==h.touches.touchend&&(h.touches.touchend=!0,h.touches.touchstart.y>-1&&Math.abs(h.touches.touchmove.y-h.touches.touchstart.y)>h.options.distance&&(h.touches.touchstart.y<h.touches.touchmove.y?h.up():h.down())));break;case"touchend":h.touches[t.type]===!1&&(h.touches[t.type]=!0,h.touches.touchstart.y>-1&&h.touches.touchmove.y>-1&&"y"===h.touches.direction&&(Math.abs(h.touches.touchmove.y-h.touches.touchstart.y)>h.options.distance&&(h.touches.touchstart.y<h.touches.touchmove.y?h.up():h.down()),h.touches.touchstart.y=-1,h.touches.touchstart.x=-1,h.touches.direction="undetermined"))}},down:function(){g<=d.length-1&&(N()&&g<d.length-1?(g++,s(g,!1)):Math.floor(p[g].height()/e(t).height())>v?(c(parseInt(d[g])+e(t).height()*v),v+=1):c(parseInt(d[g])+(p[g].height()-e(t).height())))},up:function(){g>=0&&(z()&&g>0?(g--,s(g,!1)):v>2?(v-=1,c(parseInt(d[g])+e(t).height()*v)):(v=1,c(parseInt(d[g]))))},init:function(){n.addEventListener&&(n.addEventListener("touchstart",h.touchHandler,!1),n.addEventListener("touchmove",h.touchHandler,!1),n.addEventListener("touchend",h.touchHandler,!1))}},l={handleResize:function(){clearTimeout(u),u=setTimeout(function(){D(),L(!0),H.afterResize()},400)}},H=e.extend(H,o),D(),L(!1),!0===y&&s(g,!1),a.init(),h.init(),e(t).bind("resize",l.handleResize),n.addEventListener&&t.addEventListener("orientationchange",l.handleResize,!1)},e.scrollify.move=function(e){return e===o?!1:void c(e,!1)},e.scrollify.instantMove=function(e){return e===o?!1:void c(e,!0)},e.scrollify.next=function(){g<f.length&&(g+=1,s(g,!1))},e.scrollify.previous=function(){g>0&&(g-=1,s(g,!1))},e.scrollify.instantNext=function(){g<f.length&&(g+=1,s(g,!0))},e.scrollify.instantPrevious=function(){g>0&&(g-=1,s(g,!0))},e.scrollify.destroy=function(){e(H.section).each(function(){e(this).css("height","auto")}),e(t).unbind("resize",l.handleResize),H.scrollbars&&(e(t).unbind("mousedown",a.handleMousedown),e(t).unbind("mouseup",a.handleMouseup),e(t).unbind("scroll",a.handleScroll)),e(n).unbind("DOMMouseScroll mousewheel",a.wheelHandler),e(n).unbind("keydown",a.keyHandler),n.addEventListener&&(n.removeEventListener("touchstart",h.touchHandler,!1),n.removeEventListener("touchmove",h.touchHandler,!1),n.removeEventListener("touchend",h.touchHandler,!1)),d=[],f=[],p=[],m=[]},e.scrollify.update=function(){l.handleResize()},e.scrollify.current=function(){return p[g]},e.scrollify.disable=function(){S=!0},e.scrollify.enable=function(){S=!1},e.scrollify.isDisabled=function(){return S}}(jQuery,this,document);