forked from lukehaas/Scrollify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scrollify.min.js
More file actions
29 lines (28 loc) · 10 KB
/
jquery.scrollify.min.js
File metadata and controls
29 lines (28 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*!
* jQuery Scrollify
* Version 1.0.4
*
* Requires:
* - jQuery 1.6 or higher
*
* https://github.com/lukehaas/Scrollify
*
* Copyright 2016, Luke Haas
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
!function(e,t){"use strict";"function"==typeof define&&define.amd?define(["jquery"],function(n){return t(n,e,e.document)}):"object"==typeof module&&module.exports?module.exports=function(n,o){return void 0===o&&(o="undefined"!=typeof window?require("jquery"):require("jquery")(n)),t(o,e,e.document),o}:t(jQuery,e,e.document)}("undefined"!=typeof window?window:this,function(e,t,n,o){"use strict";function i(n,o,i){if(y===n&&(i=!1),x===!0)return!0;if(f[n]){if(E=!1,i&&j.before(n,p),v=1,j.sectionName&&(I!==!0||0!==n))if(history.pushState)try{history.replaceState(null,null,f[n])}catch(s){t.console&&console.warn("Scrollify warning: This needs to be hosted on a server to manipulate the hash value.")}else t.location.hash=f[n];if(o)e(j.target).stop().scrollTop(d[n]),i&&j.after(n,p);else{if(M=!0,e().velocity?e(j.target).stop().velocity("scroll",{duration:j.scrollSpeed,easing:j.easing,offset:d[n],mobileHA:!1}):e(j.target).stop().animate({scrollTop:d[n]},j.scrollSpeed,j.easing),t.location.hash.length&&j.sectionName&&t.console)try{e(t.location.hash).length&&console.warn("Scrollify warning: There are IDs on the page that match the hash value - this will cause the page to anchor.")}catch(s){console.warn("Scrollify warning:",t.location.hash,"is not a valid jQuery expression.")}e(j.target).promise().done(function(){y=n,M=!1,I=!1,i&&j.after(n,p)})}}}function s(e){function t(t){for(var n=0,o=e.slice(Math.max(e.length-t,1)),i=0;i<o.length;i++)n+=o[i];return Math.ceil(n/t)}var n=t(10),o=t(70);return n>=o?!0:!1}function r(e,t){for(var n=f.length;n>=0;n--)"string"==typeof e?f[n]===e&&(m=n,i(n,t,!0)):n===e&&(m=n,i(n,t,!0))}var c,a,l,u,h,d=[],f=[],p=[],g=[],m=0,y=0,v=1,w=!1,b=e(t),S=b.scrollTop(),E=!1,M=!1,H=!1,x=!1,T=[],D=(new Date).getTime(),I=!0,L=!1,N="onwheel"in n?"wheel":n.onmousewheel!==o?"mousewheel":"DOMMouseScroll",j={section:".section",sectionName:"section-name",interstitialSection:"",easing:"easeOutExpo",scrollSpeed:1100,offset:0,scrollbars:!0,axis:"y",target:"html,body",standardScrollElements:!1,setHeights:!0,overflowScroll:!0,before:function(){},after:function(){},afterResize:function(){},afterRender:function(){}};e.scrollify=function(o){function r(t){e().velocity?e(j.target).stop().velocity("scroll",{duration:j.scrollSpeed,easing:j.easing,offset:t,mobileHA:!1}):e(j.target).stop().animate({scrollTop:t},j.scrollSpeed,j.easing)}function y(){var t=j.section;g=[],j.interstitialSection.length&&(t+=","+j.interstitialSection),e(t).each(function(t){j.setHeights?e(this).is(j.interstitialSection)?g[t]=!1:e(this).css("height","auto").outerHeight()<b.height()||"hidden"===e(this).css("overflow")?(e(this).css({height:b.height()}),g[t]=!1):(e(this).css({height:e(this).height()}),j.overflowScroll?g[t]=!0:g[t]=!1):e(this).outerHeight()<b.height()||j.overflowScroll===!1?g[t]=!1:g[t]=!0})}function I(n){var o=j.section;j.interstitialSection.length&&(o+=","+j.interstitialSection),d=[],f=[],p=[],e(o).each(function(n){n>0?d[n]=parseInt(e(this).offset().top)+j.offset:d[n]=parseInt(e(this).offset().top),j.sectionName&&e(this).data(j.sectionName)?f[n]="#"+e(this).data(j.sectionName).replace(/ /g,"-"):e(this).is(j.interstitialSection)===!1?f[n]="#"+(n+1):(f[n]="#",n===e(o).length-1&&n>1&&(d[n]=d[n-1]+parseInt(e(this).height()))),p[n]=e(this);try{e(f[n]).length&&t.console&&console.warn("Scrollify warning: Section names can't match IDs on the page - this will cause the browser to anchor.")}catch(i){}t.location.hash===f[n]&&(m=n,w=!0)}),!0===n?i(m,!1,!1):j.afterRender()}function k(){return g[m]?(S=b.scrollTop(),S>parseInt(d[m])?!1:!0):!0}function z(){return g[m]?(S=b.scrollTop(),S<parseInt(d[m])+(p[m].outerHeight()-b.height())-28?!1:!0):!0}L=!0,e.easing.easeOutExpo=function(e,t,n,o,i){return t==i?n+o:o*(-Math.pow(2,-10*t/i)+1)+n},l={handleMousedown:function(){return x===!0?!0:(E=!1,void(H=!1))},handleMouseup:function(){return x===!0?!0:(E=!0,void(H&&l.calculateNearest()))},handleScroll:function(){return x===!0?!0:(c&&clearTimeout(c),void(c=setTimeout(function(){return H=!0,E===!1?!1:(E=!1,void l.calculateNearest())},200)))},calculateNearest:function(){S=b.scrollTop();for(var e,t=1,n=d.length,o=0,s=Math.abs(d[0]-S);n>t;t++)e=Math.abs(d[t]-S),s>e&&(s=e,o=t);(z()||k())&&(m=o,i(o,!1,!0))},wheelHandler:function(n,o){if(x===!0)return!0;if(j.standardScrollElements&&(e(n.target).is(j.standardScrollElements)||e(n.target).closest(j.standardScrollElements).length))return!0;g[m]||n.preventDefault();var r=(new Date).getTime();n=n||t.event;var c=n.originalEvent.wheelDelta||-n.originalEvent.deltaY||-n.originalEvent.detail,o=Math.max(-1,Math.min(1,c));if(T.length>149&&T.shift(),T.push(Math.abs(c)),r-D>200&&(T=[]),D=r,M)return!1;if(0>o){if(m<d.length-1&&z()){if(!s(T))return!1;n.preventDefault(),m++,M=!0,i(m,!1,!0)}}else if(o>0&&m>0&&k()){if(!s(T))return!1;n.preventDefault(),m--,M=!0,i(m,!1,!0)}},keyHandler:function(e){return x===!0?!0:M===!0?!1:void(38==e.keyCode?m>0&&k()&&(m--,i(m,!1,!0)):40==e.keyCode&&m<d.length-1&&z()&&(m++,i(m,!1,!0)))},init:function(){j.scrollbars?(b.bind("mousedown",l.handleMousedown),b.bind("mouseup",l.handleMouseup),b.bind("scroll",l.handleScroll)):e("body").css({overflow:"hidden"}),e(n).bind(N,l.wheelHandler),e(n).bind("keydown",l.keyHandler)}},u={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(x===!0)return!0;if(j.standardScrollElements&&(e(t.target).is(j.standardScrollElements)||e(t.target).closest(j.standardScrollElements).length))return!0;var n;if("undefined"!=typeof t&&"undefined"!=typeof t.touches)switch(n=t.touches[0],t.type){case"touchstart":u.touches.touchstart.y=n.pageY,u.touches.touchmove.y=-1,u.touches.touchstart.x=n.pageX,u.touches.touchmove.x=-1,u.options.timeStamp=(new Date).getTime(),u.touches.touchend=!1;case"touchmove":u.touches.touchmove.y=n.pageY,u.touches.touchmove.x=n.pageX,u.touches.touchstart.y!==u.touches.touchmove.y&&Math.abs(u.touches.touchstart.y-u.touches.touchmove.y)>Math.abs(u.touches.touchstart.x-u.touches.touchmove.x)&&(t.preventDefault(),u.touches.direction="y",u.options.timeStamp+u.options.timeGap<(new Date).getTime()&&0==u.touches.touchend&&(u.touches.touchend=!0,u.touches.touchstart.y>-1&&Math.abs(u.touches.touchmove.y-u.touches.touchstart.y)>u.options.distance&&(u.touches.touchstart.y<u.touches.touchmove.y?u.up():u.down())));break;case"touchend":u.touches[t.type]===!1&&(u.touches[t.type]=!0,u.touches.touchstart.y>-1&&u.touches.touchmove.y>-1&&"y"===u.touches.direction&&(Math.abs(u.touches.touchmove.y-u.touches.touchstart.y)>u.options.distance&&(u.touches.touchstart.y<u.touches.touchmove.y?u.up():u.down()),u.touches.touchstart.y=-1,u.touches.touchstart.x=-1,u.touches.direction="undetermined"))}},down:function(){m<=d.length-1&&(z()&&m<d.length-1?(m++,i(m,!1,!0)):Math.floor(p[m].height()/b.height())>v?(r(parseInt(d[m])+b.height()*v),v+=1):r(parseInt(d[m])+(p[m].height()-b.height())))},up:function(){m>=0&&(k()&&m>0?(m--,i(m,!1,!0)):v>2?(v-=1,r(parseInt(d[m])+b.height()*v)):(v=1,r(parseInt(d[m]))))},init:function(){n.addEventListener&&(n.addEventListener("touchstart",u.touchHandler,!1),n.addEventListener("touchmove",u.touchHandler,!1),n.addEventListener("touchend",u.touchHandler,!1))}},h={refresh:function(e){clearTimeout(a),a=setTimeout(function(){y(),I(!0),e&&j.afterResize()},400)},handleUpdate:function(){h.refresh(!1)},handleResize:function(){h.refresh(!0)}},j=e.extend(j,o),y(),I(!1),!0===w?i(m,!1,!0):setTimeout(function(){i(0,!1,!0)},200),d.length&&(l.init(),u.init(),b.bind("resize",h.handleResize),n.addEventListener&&t.addEventListener("orientationchange",h.handleResize,!1))},e.scrollify.move=function(t){return t===o?!1:(t.originalEvent&&(t=e(this).attr("href")),void r(t,!1))},e.scrollify.instantMove=function(e){return e===o?!1:void r(e,!0)},e.scrollify.next=function(){m<f.length&&(m+=1,i(m,!1,!0))},e.scrollify.previous=function(){m>0&&(m-=1,i(m,!1,!0))},e.scrollify.instantNext=function(){m<f.length&&(m+=1,i(m,!0,!0))},e.scrollify.instantPrevious=function(){m>0&&(m-=1,i(m,!0,!0))},e.scrollify.destroy=function(){return L?(j.setHeights&&e(j.section).each(function(){e(this).css("height","auto")}),b.unbind("resize",h.handleResize),j.scrollbars&&(b.unbind("mousedown",l.handleMousedown),b.unbind("mouseup",l.handleMouseup),b.unbind("scroll",l.handleScroll)),e(n).unbind(N,l.wheelHandler),e(n).unbind("keydown",l.keyHandler),n.addEventListener&&(n.removeEventListener("touchstart",u.touchHandler,!1),n.removeEventListener("touchmove",u.touchHandler,!1),n.removeEventListener("touchend",u.touchHandler,!1)),d=[],f=[],p=[],void(g=[])):!1},e.scrollify.update=function(){return L?void h.handleUpdate():!1},e.scrollify.current=function(){return p[m]},e.scrollify.disable=function(){x=!0},e.scrollify.enable=function(){x=!1},e.scrollify.isDisabled=function(){return x},e.scrollify.setOptions=function(n){return L?void("object"==typeof n?(j=e.extend(j,n),h.handleUpdate()):t.console&&console.warn("Scrollify warning: Options need to be in an object.")):!1}});