forked from gblazex/smoothscroll-for-websites
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smoothscroll-for-websites.js
1 lines (1 loc) · 10.9 KB
/
smoothscroll-for-websites.js
1
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol?"symbol":typeof a},_createClass=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();!function(){var a="SmoothScroll",b="1.5.0",c={debug:!1,frameRate:150,animationTime:400,stepSize:100,pulseAlgorithm:!0,pulseScale:4,pulseNormalize:1,accelerationDelta:50,accelerationMax:3,keyboardSupport:!0,arrowScroll:50,touchpadSupport:!1,fixedBackground:!0,excluded:""},d={left:37,up:38,right:39,down:40,spacebar:32,pageup:33,pagedown:34,end:35,home:36},e={37:1,38:1,39:1,40:1},f=document.documentElement,g=function(){function g(a){_classCallCheck(this,g),this.options=Object.assign(c,a),this.deltaBuffer=[],window.localStorage&&localStorage.SS_deltaBuffer&&(this.deltaBuffer=localStorage.SS_deltaBuffer.split(",")),this.que=[],this.lastScroll=Date.now(),this.direction={x:0,y:0},this.cache={},this.init()}return _createClass(g,[{key:"init",value:function(){if(this.options.debug&&console.log(a+" : Initializing Instance (Binding Events)"),"onwheel"in document.createElement("div")?this.wheelEvent="wheel":"onmousewheel"in document.createElement("div")&&(this.wheelEvent="mousewheel"),this.wheelEvent&&this.constructor.BROWSER){var b=this;this.setEvent("add",this.wheelEvent,function(a){b.wheel(a)}),this.setEvent("add","mousedown",function(a){b.mousedown(a)}),this.setEvent("add","load",function(a){b.load})}}},{key:"wheel",value:function(a){this.options.debug&&console.log(this.constructor.name+" : Wheel Event"),!this.initDone&&this.load;var b=a.target,c=this.overflowingAncestor(b);if(this.isNodeName(this.activeElement,"embed")||this.isNodeName(b,"embed")&&/\.pdf/i.test(b.src)||this.isNodeName(this.activeElement,"object")||b.shadowRoot)return!0;var d=-a.wheelDeltaX||a.deltaX||0,e=-a.wheelDeltaY||a.deltaY||0;return this.constructor.isMac&&(a.wheelDeltaX&&isDivisible(a.wheelDeltaX,120)&&(d=-120*(a.wheelDeltaX/Math.abs(a.wheelDeltaX))),a.wheelDeltaY&&isDivisible(a.wheelDeltaY,120)&&(e=-120*(a.wheelDeltaY/Math.abs(a.wheelDeltaY)))),d||e||(e=-a.wheelDelta||0),1===a.deltaMode&&(d*=40,e*=40),!this.options.touchpadSupport&&this.isTouchpad(e)?!0:(Math.abs(d)>1.2&&(d*=this.options.stepSize/120),Math.abs(e)>1.2&&(e*=this.options.stepSize/120),this.scrollArray(c,d,e),a.preventDefault(),void this.scheduleClearCache())}},{key:"keydown",value:function(a){this.options.debug&&console.log(this.constructor.name+" : Keydown Event");var b=this.constructor.KEY,c=this.constructor.ARROWKEYS,d=a.target,e=a.ctrlKey||a.altKey||a.metaKey||a.shiftKey&&a.keyCode!==b.spacebar;document.body.contains(this.activeElement)||(this.activeElement=document.activeElement);var f=/^(textarea|select|embed|object)$/i,g=/^(button|submit|radio|checkbox|file|color|image)$/i;if(a.defaultPrevented||f.test(d.nodeName)||this.isNodeName(d,"input")&&!g.test(d.type)||this.isNodeName(this.activeElement,"video")||this.isInsideYoutubeVideo(a)||d.isContentEditable||e)return!0;if((this.isNodeName(d,"button")||this.isNodeName(d,"input")&&g.test(d.type))&&a.keyCode===b.spacebar)return!0;if(this.isNodeName(d,"input")&&"radio"==d.type&&c[a.keyCode])return!0;var h,i=0,j=0,k=this.overflowingAncestor(this.activeElement),l=k.clientHeight;switch(k==document.body&&(l=window.innerHeight),a.keyCode){case b.up:j=-this.options.arrowScroll;break;case b.down:j=this.options.arrowScroll;break;case b.spacebar:h=a.shiftKey?1:-1,j=-h*l*.9;break;case b.pageup:j=.9*-l;break;case b.pagedown:j=.9*l;break;case b.home:j=-k.scrollTop;break;case b.end:var m=k.scrollHeight-k.scrollTop-l;j=m>0?m+10:0;break;case b.left:i=-this.options.arrowScroll;break;case b.right:i=this.options.arrowScroll;break;default:return!0}this.scrollArray(k,i,j),a.preventDefault(),this.scheduleClearCache()}},{key:"mousedown",value:function(a){this.options.debug&&console.log(this.constructor.name+" : Mousedown Event"),this.activeElement=a.target}},{key:"initTest",value:function(){if(this.options.keyboardSupport)var a=this;this.setEvent("add","keydown",function(b){a.keydown(b)})}},{key:"isNodeName",value:function(a,b){return(a.nodeName||"").toLowerCase()===b.toLowerCase()}},{key:"setEvent",value:function(a,b,c){"add"==a?window.addEventListener(b,c,!1):"remove"==a&&window.removeEventListener(b,c,!1)}},{key:"requestFrame",value:function(a,b,c){window.setTimeout(a,c||1e3/60)}},{key:"directionCheck",value:function(a,b){a=a>0?1:-1,b=b>0?1:-1,this.direction.x===a&&this.direction.y===b||(this.direction.x=a,this.direction.y=b,this.que=[],this.lastScroll=0)}},{key:"isTouchpad",value:function(a){if(a){this.deltaBuffer.length||(this.deltaBuffer=[a,a,a]),a=Math.abs(a),this.deltaBuffer.push(a),this.deltaBuffer.shift(),clearTimeout(this.deltaBufferTimer);var b=this;return this.deltaBufferTimer=setTimeout(function(){window.localStorage&&(localStorage.SS_deltaBuffer=b.deltaBuffer.join(","))},1e3),!this.allDeltasDivisableBy(120)&&!this.allDeltasDivisableBy(100)}}},{key:"isDivisible",value:function(a,b){return Math.floor(a/b)==a/b}},{key:"allDeltasDivisableBy",value:function(a){return this.isDivisible(this.deltaBuffer[0],a)&&this.isDivisible(this.deltaBuffer[1],a)&&this.isDivisible(this.deltaBuffer[2],a)}},{key:"isInsideYoutubeVideo",value:function(a){var b=a.target,c=!1;if(-1!=document.URL.indexOf("www.youtube.com/watch"))do if(c=b.classList&&b.classList.contains("html5-video-controls"))break;while(b=b.parentNode);return c}},{key:"scrollArray",value:function(a,b,c){if(this.options.debug&&console.log(this.constructor.NAME+" : Functionality"),this.directionCheck(b,c),1!=this.options.accelerationMax){var d=Date.now(),e=d-this.lastScroll;if(e<this.options.accelerationDelta){var f=(1+50/e)/2;f>1&&(f=Math.min(f,this.options.accelerationMax),b*=f,c*=f)}this.lastScroll=Date.now()}if(this.que.push({x:b,y:c,lastX:0>b?.99:-.99,lastY:0>c?.99:-.99,start:Date.now()}),!this.pending){var g=a===document.body,h=this,i=function j(d){for(var e=Date.now(),f=0,i=0,k=0;k<h.que.length;k++){var l=h.que[k],m=e-l.start,n=m>=h.options.animationTime,o=n?1:m/h.options.animationTime;h.options.pulseAlgorithm&&(o=h.pulse(o));var p=l.x*o-l.lastX>>0,q=l.y*o-l.lastY>>0;f+=p,i+=q,l.lastX+=p,l.lastY+=q,n&&(h.que.splice(k,1),k--)}g?window.scrollBy(f,i):(f&&(a.scrollLeft+=f),i&&(a.scrollTop+=i)),b||c||(h.que=[]),h.que.length?h.requestFrame(j,a,1e3/h.options.frameRate+1):h.pending=!1};this.requestFrame(i,a,0),this.pending=!0}}},{key:"scheduleClearCache",value:function(){clearTimeout(this.clearCacheTimer),this.clearCacheTimer=setInterval(function(){this.cache={}},1e3)}},{key:"setCache",value:function(a,b){for(var c=a.length;c--;)this.cache[this.uniqueID(a[c])]=b;return b}},{key:"overflowingAncestor",value:function(a){var b=this.constructor.ROOT,c=[],d=document.body,e=b.scrollHeight;do{var f=this.cache[this.uniqueID(a)];if(f)return this.setCache(c,f);if(c.push(a),e===a.scrollHeight){var g=this.overflowNotHidden(b)&&this.overflowNotHidden(d),h=g||this.overflowAutoOrScroll(b);if(this.isFrame&&this.isContentOverflowing(b)||!this.isFrame&&h)return this.setCache(c,this.constructor.getScrollRoot)}else if(this.isContentOverflowing(a)&&this.overflowAutoOrScroll(a))return this.setCache(c,a)}while(a=a.parentElement)}},{key:"isContentOverflowing",value:function(a){return a.clientHeight+10<a.scrollHeight}},{key:"overflowNotHidden",value:function(a){var b=getComputedStyle(a,"").getPropertyValue("overflow-y");return"hidden"!==b}},{key:"overflowAutoOrScroll",value:function(a){var b=getComputedStyle(a,"").getPropertyValue("overflow-y");return"scroll"===b||"auto"===b}},{key:"uniqueID",value:function(a){var b=0;return function(a){return a.uniqueID||(a.uniqueID=b++)}}},{key:"pulse_",value:function(a){var b,c,d;return a*=this.options.pulseScale,1>a?b=a-(1-Math.exp(-a)):(c=Math.exp(-1),a-=1,d=1-Math.exp(-a),b=c+d*(1-c)),b*this.options.pulseNormalize}},{key:"pulse",value:function(a){return a>=1?1:0>=a?0:(1==this.options.pulseNormalize&&(this.options.pulseNormalize/=this.pulse_(1)),this.pulse_(a))}},{key:"load",get:function(){if(this.options.debug&&console.log(a+" : Load"),!this.initDone&&document.body){this.initDone=!0;var b=document.body,c=document.documentElement,d=window.innerHeight,e=b.scrollHeight,f=document.compatMode.indexOf("CSS")>=0?c:b;if(this.activeElement=b,this.initTest(),top!=self)this.isFrame=!0;else if(e>d&&(b.offsetHeight<=d||c.offsetHeight<=d)){var g=document.createElement("div");g.style.cssText="position:absolute; z-index:-10000; top:0; left:0; right:0; height:"+f.scrollHeight+"px",document.body.appendChild(g);var h;this.refreshSize=function(){h||(h=setTimeout(function(){this.isExcluded||(g.style.height="0",g.style.height=f.scrollHeight+"px",h=null)},500))},setTimeout(this.refreshSize,10),this.setEvent("add","resize",this.refreshSize);var i={attributes:!0,childList:!0,characterData:!1};if(this.observer=new MutationObserver(this.refreshSize),this.observer.observe(b,i),f.offsetHeight<=d){var j=document.createElement("div");j.style.clear="both",b.appendChild(j)}}this.options.fixedBackground||this.isExcluded||(b.style.backgroundAttachment="scroll",c.style.backgroundAttachment="scroll")}}},{key:"destroy",get:function(){this.options.debug&&console.log(a+" : Destroying Instance"),this.observer&&this.observer.disconnect();var b=this;this.setEvent("remove",this.wheelEvent,function(a){b.wheel(a)}),this.setEvent("remove","mousedown",function(a){b.mousedown(a)}),this.setEvent("remove","keydown",function(a){b.keydown(a)}),this.setEvent("remove","resize",function(a){b.refreshSize()}),this.setEvent("remove","load",function(a){b.load})}}],[{key:"_invoke",value:function(a){new g(a)}},{key:"NAME",get:function(){return a}},{key:"VERSION",get:function(){return b}},{key:"DEFAULTS",get:function(){return c}},{key:"KEY",get:function(){return d}},{key:"ARROWKEYS",get:function(){return e}},{key:"BROWSER",get:function(){var a=window.navigator.userAgent,b=/Edge/.test(a),c=/chrome/i.test(a)&&!b,d=/safari/i.test(a)&&!b,e=/mobile/i.test(a),f=/Windows NT 6.1/i.test(a)&&/rv:11/i.test(a);return(c||d||f)&&!e}},{key:"ROOT",get:function(){return f}},{key:"isMac",get:function(){return/^Mac/.test(navigator.platform)}},{key:"getScrollRoot",get:function(){var a,b=function(){if(!a){var b=document.createElement("div");b.style.cssText="height:10000px;width:1px;",document.body.appendChild(b);var c=document.body.scrollTop;document.documentElement.scrollTop;window.scrollBy(0,3),a=document.body.scrollTop!=c?document.body:document.documentElement,window.scrollBy(0,-3),document.body.removeChild(b)}return a}();return b}}]),g}();window.SmoothScroll=g._invoke,window.SmoothScroll.Constructor=g,window.SmoothScrollOptions&&g._invoke(window.SmoothScrollOptions),"function"==typeof define&&define.amd?define(function(){return g._invoke}):"object"==("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=g._invoke:g._invoke(window.SmoothScrollOptions)}();