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