smooth-scrollbar
Version:
Customize scrollbar in modern browsers with smooth scrolling experience.
16 lines (14 loc) • 12.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("smooth-scrollbar")):"function"==typeof define&&define.amd?define(["smooth-scrollbar"],e):"object"==typeof exports?exports.OverscrollPlugin=e(require("smooth-scrollbar")):t.OverscrollPlugin=e(t.Scrollbar)}(this,(function(t){return function(t){var e={};function o(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,o),n.l=!0,n.exports}return o.m=t,o.c=e,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=3)}([function(t,e){var o=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,n=/^0b[01]+$/i,i=/^0o[0-7]+$/i,s=parseInt,c=Object.prototype.toString;function l(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function a(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==c.call(t)}(t))return NaN;if(l(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=l(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(o,"");var a=n.test(t);return a||i.test(t)?s(t.slice(2),a?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,o){return void 0===o&&(o=e,e=void 0),void 0!==o&&(o=(o=a(o))==o?o:0),void 0!==e&&(e=(e=a(e))==e?e:0),function(t,e,o){return t==t&&(void 0!==o&&(t=t<=o?t:o),void 0!==e&&(t=t>=e?t:e)),t}(a(t),e,o)}},function(t,e,o){(function(e){var o=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,n=/^0b[01]+$/i,i=/^0o[0-7]+$/i,s=parseInt,c="object"==typeof e&&e&&e.Object===Object&&e,l="object"==typeof self&&self&&self.Object===Object&&self,a=c||l||Function("return this")(),u=Object.prototype.toString,f=Math.max,h=Math.min,p=function(){return a.Date.now()};function d(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function v(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==u.call(t)}(t))return NaN;if(d(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=d(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(o,"");var c=n.test(t);return c||i.test(t)?s(t.slice(2),c?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,o){var r,n,i,s,c,l,a=0,u=!1,y=!1,_=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function b(e){var o=r,i=n;return r=n=void 0,a=e,s=t.apply(i,o)}function m(t){var o=t-l;return void 0===l||o>=e||o<0||y&&t-a>=i}function x(){var t=p();if(m(t))return g(t);c=setTimeout(x,function(t){var o=e-(t-l);return y?h(o,i-(t-a)):o}(t))}function g(t){return c=void 0,_&&r?b(t):(r=n=void 0,s)}function O(){var t=p(),o=m(t);if(r=arguments,n=this,l=t,o){if(void 0===c)return function(t){return a=t,c=setTimeout(x,e),u?b(t):s}(l);if(y)return c=setTimeout(x,e),b(l)}return void 0===c&&(c=setTimeout(x,e)),s}return e=v(e)||0,d(o)&&(u=!!o.leading,i=(y="maxWait"in o)?f(v(o.maxWait)||0,e):i,_="trailing"in o?!!o.trailing:_),O.cancel=function(){void 0!==c&&clearTimeout(c),a=0,r=l=n=c=void 0},O.flush=function(){return void 0===c?s:g(p())},O}}).call(this,o(4))},function(e,o){e.exports=t},function(t,e,o){t.exports=o(5)},function(t,e){var o;o=function(){return this}();try{o=o||new Function("return this")()}catch(t){"object"==typeof window&&(o=window)}t.exports=o},function(t,e,o){"use strict";o.r(e);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,e)},n=function(){return(n=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)},i=o(0),s=o.n(i),c=o(1),l=o.n(c),a=o(2),u=["webkit","moz","ms","o"],f=new RegExp("^-(?!(?:"+u.join("|")+")-)");function h(t,e){e=function(t){var e={};return Object.keys(t).forEach((function(o){if(f.test(o)){var r=t[o];o=o.replace(/^-/,""),e[o]=r,u.forEach((function(t){e["-"+t+"-"+o]=r}))}else e[o]=t[o]})),e}(e),Object.keys(e).forEach((function(o){var r=o.replace(/^-/,"").replace(/-([a-z])/g,(function(t,e){return e.toUpperCase()}));t.style[r]=e[o]}))}var p,d=function(){function t(t){this._scrollbar=t}return t.prototype.render=function(t){var e=t.x,o=void 0===e?0:e,r=t.y,n=void 0===r?0:r,i=this._scrollbar,s=i.size,c=i.track,l=i.offset;if(h(i.contentEl,{"-transform":"translate3d("+-(l.x+o)+"px, "+-(l.y+n)+"px, 0)"}),o){c.xAxis.show();var a=s.container.width/(s.container.width+Math.abs(o));h(c.xAxis.thumb.element,{"-transform":"translate3d("+c.xAxis.thumb.offset+"px, 0, 0) scale3d("+a+", 1, 1)","-transform-origin":o<0?"left":"right"})}n&&(c.yAxis.show(),a=s.container.height/(s.container.height+Math.abs(n)),h(c.yAxis.thumb.element,{"-transform":"translate3d(0, "+c.yAxis.thumb.offset+"px, 0) scale3d(1, "+a+", 1)","-transform-origin":n<0?"top":"bottom"})),c.autoHideOnIdle()},t}(),v=function(){function t(t){this._scrollbar=t,this._canvas=document.createElement("canvas"),this._ctx=this._canvas.getContext("2d"),h(this._canvas,{position:"absolute",top:0,left:0,width:"100%",height:"100%",display:"none"})}return t.prototype.mount=function(){this._scrollbar.containerEl.appendChild(this._canvas)},t.prototype.unmount=function(){this._canvas.parentNode&&this._canvas.parentNode.removeChild(this._canvas)},t.prototype.adjust=function(){var t=this._scrollbar.size,e=window.devicePixelRatio||1,o=t.container.width*e,r=t.container.height*e;o===this._canvas.width&&r===this._canvas.height||(this._canvas.width=o,this._canvas.height=r,this._ctx.scale(e,e))},t.prototype.recordTouch=function(t){var e=t.touches[t.touches.length-1];this._touchX=e.clientX,this._touchY=e.clientY},t.prototype.render=function(t,e){var o=t.x,r=void 0===o?0:o,n=t.y,i=void 0===n?0:n;if(r||i){h(this._canvas,{display:"block"});var s=this._scrollbar.size;this._ctx.clearRect(0,0,s.container.width,s.container.height),this._ctx.fillStyle=e,this._renderX(r),this._renderY(i)}else h(this._canvas,{display:"none"})},t.prototype._getMaxOverscroll=function(){var t=this._scrollbar.options.plugins.overscroll;return t&&t.maxOverscroll?t.maxOverscroll:150},t.prototype._renderX=function(t){var e=this._scrollbar.size,o=this._getMaxOverscroll(),r=e.container,n=r.width,i=r.height,c=this._ctx;c.save(),t>0&&c.transform(-1,0,0,1,n,0);var l=s()(Math.abs(t)/o,0,.75),a=s()(l,0,.25)*n,u=Math.abs(t),f=this._touchY||i/2;c.globalAlpha=l,c.beginPath(),c.moveTo(0,-a),c.quadraticCurveTo(u,f,0,i+a),c.fill(),c.closePath(),c.restore()},t.prototype._renderY=function(t){var e=this._scrollbar.size,o=this._getMaxOverscroll(),r=e.container,n=r.width,i=r.height,c=this._ctx;c.save(),t>0&&c.transform(1,0,0,-1,0,i);var l=s()(Math.abs(t)/o,0,.75),a=s()(l,0,.25)*n,u=this._touchX||n/2,f=Math.abs(t);c.globalAlpha=l,c.beginPath(),c.moveTo(-a,0),c.quadraticCurveTo(u,f,n+a,0),c.fill(),c.closePath(),c.restore()},t}();o.d(e,"OverscrollEffect",(function(){return p})),function(t){t.BOUNCE="bounce",t.GLOW="glow"}(p||(p={}));var y=/wheel|touch/,_=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._glow=new v(e.scrollbar),e._bounce=new d(e.scrollbar),e._wheelScrollBack={x:!1,y:!1},e._lockWheel={x:!1,y:!1},e._touching=!1,e._amplitude={x:0,y:0},e._position={x:0,y:0},e._releaseWheel=l()((function(){e._lockWheel.x=!1,e._lockWheel.y=!1}),30),e}return function(t,e){function o(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(e,t),Object.defineProperty(e.prototype,"_isWheelLocked",{get:function(){return this._lockWheel.x||this._lockWheel.y},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"_enabled",{get:function(){return!!this.options.effect},enumerable:!0,configurable:!0}),e.prototype.onInit=function(){var t=this._glow,e=this.options,o=this.scrollbar,r=e.effect;Object.defineProperty(e,"effect",{get:function(){return r},set:function(e){if(e){if(e!==p.BOUNCE&&e!==p.GLOW)throw new TypeError("unknow overscroll effect: "+e);r=e,o.options.continuousScrolling=!1,e===p.GLOW?(t.mount(),t.adjust()):t.unmount()}else r=void 0}}),e.effect=r},e.prototype.onUpdate=function(){this.options.effect===p.GLOW&&this._glow.adjust()},e.prototype.onRender=function(t){if(this._enabled){this.scrollbar.options.continuousScrolling&&(this.scrollbar.options.continuousScrolling=!1);var e=t.x,o=t.y;!this._amplitude.x&&this._willOverscroll("x",t.x)&&(e=0,this._absorbMomentum("x",t.x)),!this._amplitude.y&&this._willOverscroll("y",t.y)&&(o=0,this._absorbMomentum("y",t.y)),this.scrollbar.setMomentum(e,o),this._render()}},e.prototype.transformDelta=function(t,e){if(this._lastEventType=e.type,!this._enabled||!y.test(e.type))return t;this._isWheelLocked&&/wheel/.test(e.type)&&(this._releaseWheel(),this._willOverscroll("x",t.x)&&(t.x=0),this._willOverscroll("y",t.y)&&(t.y=0));var o=t.x,r=t.y;switch(this._willOverscroll("x",t.x)&&(o=0,this._addAmplitude("x",t.x)),this._willOverscroll("y",t.y)&&(r=0,this._addAmplitude("y",t.y)),e.type){case"touchstart":case"touchmove":this._touching=!0,this._glow.recordTouch(e);break;case"touchcancel":case"touchend":this._touching=!1}return{x:o,y:r}},e.prototype._willOverscroll=function(t,e){if(!e)return!1;if(this._position[t])return!0;var o=this.scrollbar.offset[t],r=this.scrollbar.limit[t];return 0!==r&&s()(o+e,0,r)===o&&(0===o||o===r)},e.prototype._absorbMomentum=function(t,e){var o=this.options,r=this._lastEventType,n=this._amplitude;y.test(r)&&(n[t]=s()(e,-o.maxOverscroll,o.maxOverscroll))},e.prototype._addAmplitude=function(t,e){var o=this.options,r=this.scrollbar,n=this._amplitude,i=this._position,c=n[t],l=e*c<0,a=c+e*(1-(l?0:this._wheelScrollBack[t]?1:Math.abs(c/o.maxOverscroll)));n[t]=0===r.offset[t]?s()(a,-o.maxOverscroll,0):s()(a,0,o.maxOverscroll),l&&(i[t]=n[t])},e.prototype._render=function(){var t=this.options,e=this._amplitude,o=this._position;if(this._enabled&&(e.x||e.y||o.x||o.y)){var r=this._nextAmp("x"),i=this._nextAmp("y");switch(e.x=r.amplitude,o.x=r.position,e.y=i.amplitude,o.y=i.position,t.effect){case p.BOUNCE:this._bounce.render(o);break;case p.GLOW:this._glow.render(o,this.options.glowColor)}"function"==typeof t.onScroll&&t.onScroll.call(this,n({},o))}},e.prototype._nextAmp=function(t){var e=this.options,o=this._amplitude,r=this._position,n=1-e.damping,i=o[t],s=r[t],c=this._touching?i:i*n|0,l=c-s,a=s+l-(l*n|0);return!this._touching&&Math.abs(a)<Math.abs(s)&&(this._wheelScrollBack[t]=!0),this._wheelScrollBack[t]&&Math.abs(a)<=1&&(this._wheelScrollBack[t]=!1,this._lockWheel[t]=!0),{amplitude:c,position:a}},e.pluginName="overscroll",e.defaultOptions={effect:p.BOUNCE,onScroll:void 0,damping:.2,maxOverscroll:150,glowColor:"#87ceeb"},e}(a.ScrollbarPlugin);e.default=_}]).default}));