@scena/dragscroll
Version:
11 lines (10 loc) • 5.92 kB
JavaScript
/*
Copyright (c) 2019 Daybrush
name: @scena/dragscroll
license: MIT
author: Daybrush
repository: git+https://github.com/daybrush/dragscroll.git
version: 1.4.0
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).DragScroll=e()}(this,function(){"use strict";var o=function(t,e){return(o=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(t,e){t.__proto__=e}:function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])}))(t,e)};var u=function(){return(u=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)},e="function",n="string";function s(t){return t&&"object"==typeof t}var r=function(){return(r=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function c(t){t=t.container;return t===document.body?[t.scrollLeft||document.documentElement.scrollLeft,t.scrollTop||document.documentElement.scrollTop]:[t.scrollLeft,t.scrollTop]}function l(t,e){return t.addEventListener("scroll",e),function(){t.removeEventListener("scroll",e)}}function a(t){return t?typeof t===n?document.querySelector(t):typeof t===e?t():t instanceof Element?t:"current"in t?t.current:"value"in t?t.value:void 0:null}return function(t){function e(){this.constructor=n}var n;function i(){var n=null!==t&&t.apply(this,arguments)||this;return n._startRect=null,n._startPos=[],n._prevTime=0,n._timer=0,n._prevScrollPos=[0,0],n._isWait=!1,n._flag=!1,n._currentOptions=null,n._lock=!1,n._unregister=null,n._onScroll=function(){var e=n._currentOptions;!n._lock&&e&&n.emit("scrollDrag",{next:function(t){n.checkScroll({container:e.container,inputEvent:t})}})},n}o(n=i,r=t),n.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e);var r=i.prototype;return r.dragStart=function(t,e){var n,i,r,o,s=a(e.container);s?(o=r=i=n=0,o=s===document.body?(r=window.innerWidth,window.innerHeight):(n=(s=s.getBoundingClientRect()).top,i=s.left,r=s.width,s.height),this._flag=!0,this._startPos=[t.clientX,t.clientY],this._startRect={top:n,left:i,width:r,height:o},this._prevScrollPos=this._getScrollPosition([0,0],e),this._currentOptions=e,this._registerScrollEvent(e)):this._flag=!1},r.drag=function(t,e){var n,i,r,o,s,c;if(clearTimeout(this._timer),this._flag)return n=t.clientX,i=t.clientY,r=void 0===(r=e.threshold)?0:r,o=this._startRect,s=this._startPos,this._currentOptions=e,c=[0,0],o.top>i-r?(s[1]>o.top||i<s[1])&&(c[1]=-1):o.top+o.height<i+r&&(s[1]<o.top+o.height||i>s[1])&&(c[1]=1),o.left>n-r?(s[0]>o.left||n<s[0])&&(c[0]=-1):o.left+o.width<n+r&&(s[0]<o.left+o.width||n>s[0])&&(c[0]=1),!(!c[0]&&!c[1])&&this._continueDrag(u(u({},e),{direction:c,inputEvent:t,isDrag:!0}))},r.checkScroll=function(t){var e,n,i,r,o,s,c,l=this;return!(this._isWait||(s=void 0===(s=t.prevScrollPos)?this._prevScrollPos:s,c=t.direction,e=void 0===(e=t.throttleTime)?0:e,n=t.inputEvent,i=t.isDrag,o=(r=this._getScrollPosition(c||[0,0],t))[0]-s[0],s=r[1]-s[1],c=c||[o?Math.abs(o)/o:0,s?Math.abs(s)/s:0],this._prevScrollPos=r,this._lock=!1,!o&&!s)||(this.emit("move",{offsetX:c[0]?o:0,offsetY:c[1]?s:0,inputEvent:n}),e&&i&&(clearTimeout(this._timer),this._timer=window.setTimeout(function(){l._continueDrag(t)},e)),0))},r.dragEnd=function(){this._flag=!1,this._lock=!1,clearTimeout(this._timer),this._unregisterScrollEvent()},r._getScrollPosition=function(t,e){var n=e.container,e=e.getScrollPosition;return(void 0===e?c:e)({container:a(n),direction:t})},r._continueDrag=function(t){var e=this,n=t.container,i=t.direction,r=t.throttleTime,o=t.useScroll,s=t.isDrag,c=t.inputEvent;if(!(!this._flag||s&&this._isWait)){var l=Date.now?Date.now():(new Date).getTime(),r=Math.max(r+this._prevTime-l,0);if(0<r)return clearTimeout(this._timer),this._timer=window.setTimeout(function(){e._continueDrag(t)},r),!1;this._prevTime=l;r=this._getScrollPosition(i,t),l=(this._prevScrollPos=r,s&&(this._isWait=!0),o||(this._lock=!0),{container:a(n),direction:i,inputEvent:c});return null!=(s=t.requestScroll)&&s.call(t,l),this.emit("scroll",l),this._isWait=!1,o||this.checkScroll(u(u({},t),{prevScrollPos:r,direction:i,inputEvent:c}))}},r._registerScrollEvent=function(t){this._unregisterScrollEvent();var e,n=t.checkScrollEvent;n&&(e=!0===n?l:n,t=a(t.container),!0!==n||t!==document.body&&t!==document.documentElement?this._unregister=e(t,this._onScroll):this._unregister=l(window,this._onScroll))},r._unregisterScrollEvent=function(){var t;null!=(t=this._unregister)&&t.call(this),this._unregister=null},i}(function(){function t(){this._events={}}var e=t.prototype;return e.on=function(t,e){if(s(t))for(var n in t)this.on(n,t[n]);else this._addEvent(t,e,{});return this},e.off=function(t,e){if(t)if(s(t))for(var n in t)this.off(n);else{var i,r;e?(i=this._events[t])&&-1<(r=function(t,e,n){void 0===n&&(n=-1);for(var i=t.length,r=0;r<i;++r)if(e(t[r],r,t))return r;return n}(i,function(t){return t.listener===e}))&&i.splice(r,1):this._events[t]=[]}else this._events={};return this},e.once=function(e,t){var n=this;return t&&this._addEvent(e,t,{once:!0}),new Promise(function(t){n._addEvent(e,t,{once:!0})})},e.emit=function(e,n){var t,i=this,r=(void 0===n&&(n={}),this._events[e]);return!e||!r||(t=!1,n.eventType=e,n.stop=function(){t=!0},n.currentTarget=this,function(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;for(var i=Array(t),r=0,e=0;e<n;e++)for(var o=arguments[e],s=0,c=o.length;s<c;s++,r++)i[r]=o[s];return i}(r).forEach(function(t){t.listener(n),t.once&&i.off(e,t.listener)}),!t)},e.trigger=function(t,e){return this.emit(t,e=void 0===e?{}:e)},e._addEvent=function(t,e,n){var i=this._events;i[t]=i[t]||[],i[t].push(r({listener:e},n))},t}())});
//# sourceMappingURL=dragscroll.min.js.map