-
Notifications
You must be signed in to change notification settings - Fork 1
/
smooth-scroll.min.js
1 lines (1 loc) · 5.17 KB
/
smooth-scroll.min.js
1
'use strict';Object.defineProperty(exports,'__esModule',{value:!0});function _toConsumableArray(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)}var SmoothScroll=function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:null;this.DOM={},this.config={},this.move={},this.prlxItems=[],this.init(a,b)};SmoothScroll.prototype=function(){var a=function(v){v.preventDefault(),this.move.destY+=this.runFirefox&&1==v.deltaMode?v.deltaY*this.config.speed*this.config.multFirefox:v.deltaY*this.config.speed,h.call(this)},c=function(v){var w=v.targetTouches?v.targetTouches[0]:v;this.move.touchY=w.pageY},d=function(v){v.preventDefault();var w=v.targetTouches?v.targetTouches[0]:v;this.move.destY+=(w.pageY-this.move.touchY)*this.config.touchSpeed,this.move.touchY=w.pageY,h.call(this)},f=function(v){(38===v.keyCode||40===v.keyCode)&&v.preventDefault(),this.move.destY+=38===v.keyCode?-this.config.jump:40===v.keyCode?this.config.jump:0,h.call(this)},g=function(){this.move.destY=window.scrollY||window.pageYOffset,h.call(this)},h=function(){this.config.ticking||(this.rAF=requestAnimationFrame(j.bind(this)),this.config.ticking=!0)},j=function(){if(this.rAF=requestAnimationFrame(j.bind(this)),this.move.destY=this.prevent||Math.round(Math.max(0,Math.min(this.move.destY,this.config.scrollMax))),this.move.prevY!==this.move.destY){if(this.prevent)return;this.move.currentY+=(this.move.destY-this.move.currentY)*this.config.delay;var v=-this.move.currentY.toFixed(2);this.DOM.scroller.style.transform=this.enableSmoothScroll&&!this.prevent&&'translate3D(0,'+v+'px, 0)',this.prlxItems&&this.prlxItems.update(v),this.move.prevY=Math.round(this.move.currentY)}else this.config.ticking=!1,cancelAnimationFrame(this.rAF)},k=function(){var v=0<arguments.length&&void 0!==arguments[0]?arguments[0]:'bind',w='bind'===v?'addEventListener':'unbind'===v?'removeEventListener':null;if(null===w)throw'_domEvent function - wrong method! expect \'bind\' || \'unbind\' : got '+v;this.enableSmoothScroll?(this.deviceHasEvents.wheel&&(this._wheelFunc||(this._wheelFunc=a.bind(this)))&&document[w]('wheel',this._wheelFunc,!1),this.deviceHasEvents.mouseWheel&&(this._mouWheelFunc||(this._mouWheelFunc=a.bind(this)))&&document[w]('mousewheel',this._mouWheelFunc,!1),this.deviceHasEvents.keys&&(this._keysFunc||(this._keysFunc=f.bind(this)))&&document[w]('keydown',this._keysFunc,!1),this.deviceHasEvents.touch&&(!this._touchStatFunc&&(this._touchStatFunc=c.bind(this)),!this._touchMoveFunc&&(this._touchMoveFunc=d.bind(this)),document[w]('touchstart',this._touchStatFunc),document[w]('touchmove',this._touchMoveFunc))):this.config.parallax&&(!this._scrollFunc&&(this._scrollFunc=g.bind(this)),document[w]('scroll',this._scrollFunc,!1))},l=function(){var z=this,v=[].concat(_toConsumableArray(this.DOM.scroller.querySelectorAll('img, video')));if(0>=v.length)return void(this.config.scrollMax=this.DOM.scroller.offsetHeight-(document.documentElement.clientHeight||window.innerHeight));var w=!!window.Promise,x=w?[]:null,y=function(){z.config.scrollMax=z.DOM.scroller.offsetHeight-(document.documentElement.clientHeight||window.innerHeight)};v.forEach(function(A,B,C){var D='img'===A.nodeName.toLowerCase()?'load':'loadstart',E=document.createElement(A.nodeName.toLowerCase());if(w){var F=new Promise(function(G){E.addEventListener(D,function(){G()},!1)});x.push(F)}else E.onloadstart=E.onload=function(){C.splice(C.indexOf(A),1),0===C.length&&y()};E.src=A.src}),w&&Promise.all(x).then(function(){y()})},m=function(){return this.config.fixedClass?this.DOM.container.classList.add(this.config.fixedClass):(this.DOM.container.style.overflow='hidden',this.DOM.container.style.height='100vh'),!0},n=function(){return{wheel:'onwheel'in document,mouseWheel:'onmousewheel'in document,touch:'ontouchstart'in document,keys:'onkeydown'in document}},q=function(){this.enableSmoothScroll&&m.call(this),k.call(this,'bind')};return destroy=function destroy(){for(var v in this.prlx=this.prlx.destroy(),delete this.prlx,this.unbindEvent.call(this),this)Object.prototype.hasOwnProperty.call(this,v)&&(this[v]=null,delete this[v]);return null},{init:function o(v,w){this.DOM.scroller=document.querySelector('.js-scroller'),this.DOM.container=this.DOM.scroller.parentNode,this.config={delay:v.delay||.1,speed:v.speed||1,touchSpeed:v.touchSpeed||1.5,jump:v.jump||110,parallax:v.parallax||!1,touch:v.touch||!1,fixedClass:w||!1,multFirefox:15,scrollMax:0,ticking:!1},this.move={currentY:0,destY:0,prevY:0,touchY:0},l.call(this),this.runFirefox=-1<navigator.userAgent.indexOf('Firefox'),this.deviceHasEvents=n(),this.enableSmoothScroll=!this.deviceHasEvents.touch||this.config.touch,this.prlxItems=this.config.parallax?new Parallax:null,q.call(this)},start:function p(){this.prevent=!1},resize:function u(){this.config.scrollMax=this.DOM.scroller.offsetHeight-(document.documentElement.clientHeight||window.innerHeight)},bindEvent:q,unbindEvent:function r(){k.call(this,'unbind'),'undefined'!=typeof this.rAF&&(cancelAnimationFrame(this.rAF),this.rAF=null)},destroy:destroy}}(),Object.defineProperty(SmoothScroll.prototype,'preventScroll',{set:function set(a){this.prevent=a}}),exports.SmoothScroll=SmoothScroll;