wheel.min.js 6.1 KB

1
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Wheel=t()}(this,function(){"use strict";var n="undefined"!=typeof window,t=n&&navigator.userAgent.toLowerCase();t&&/wechatdevtools/.test(t),t&&t.indexOf("android"),function(){if("string"==typeof t){var e=/os (\d\d?_\d(_\d)?)/.exec(t);if(!e)return;e=e[1].split("_").map(function(e){return parseInt(e,10)});13===e[0]&&e[1]}}();if(n)try{var e={};Object.defineProperty(e,"passive",{get:function(){}}),window.addEventListener("test-passive",function(){},e)}catch(e){}var s=n&&document.createElement("div").style,o=function(){if(!n)return!1;for(var e=0,t=[{key:"standard",value:"transform"},{key:"webkit",value:"webkitTransform"},{key:"Moz",value:"MozTransform"},{key:"O",value:"OTransform"},{key:"ms",value:"msTransform"}];e<t.length;e++){var o=t[e];if(void 0!==s[o.value])return o.key}return!1}();function i(e){return!1===o?e:"standard"===o?"transitionEnd"===e?"transitionend":e:o+e.charAt(0).toUpperCase()+e.substr(1)}o&&"standard"!==o&&o.toLowerCase();var r=i("transform");i("transition");n&&i("perspective")in s;var l=r,a=i("transitionTimingFunction"),h=i("transitionDuration");i("transitionDelay"),i("transformOrigin"),i("transitionEnd"),i("transitionProperty");function c(e,t){return new RegExp("(^|\\s)"+t+"(\\s|$)").test(e.className)}var d={style:"cubic-bezier(0.23, 1, 0.32, 1)",fn:function(e){return 1+--e*e*e*e*e}},u=[{key:"wheelTo",name:"wheelTo"},{key:"getSelectedIndex",name:"getSelectedIndex"},{key:"restorePosition",name:"restorePosition"}].map(function(e){return{key:e.key,sourceKey:"plugins.wheel."+e.name}}),f="wheelIndexChanged",p=4;function m(e){this.scroll=e,this.init()}return m.prototype.init=function(){this.handleBScroll(),this.handleOptions(),this.handleHooks(),this.refreshBoundary(),this.setSelectedIndex(this.options.selectedIndex)},m.prototype.handleBScroll=function(){this.scroll.proxy(u),this.scroll.registerType([f])},m.prototype.handleOptions=function(){var e=!0===this.scroll.options.wheel?{}:this.scroll.options.wheel;this.options=function(e,t){for(var o in t)e[o]=t[o];return e}({wheelWrapperClass:"wheel-scroll",wheelItemClass:"wheel-item",rotate:25,adjustTime:400,selectedIndex:0,wheelDisabledItemClass:"wheel-disabled-item"},e)},m.prototype.handleHooks=function(){var o=this,e=this.scroll,t=this.scroll.scroller,n=t.actionsHandler,s=t.scrollBehaviorX,i=t.scrollBehaviorY,r=t.animater,l=t.content;e.on(e.eventTypes.scrollEnd,function(e){e=o.findNearestValidWheel(e.y).index;if(t.animater.forceStopped&&!o.isAdjustingPosition)return o.target=o.items[e],!0;o.setSelectedIndex(e),o.isAdjustingPosition&&(o.isAdjustingPosition=!1)}),this.scroll.hooks.on(this.scroll.hooks.eventTypes.refresh,function(e){e!==l&&(l=e,o.setSelectedIndex(o.options.selectedIndex,!0)),o.rotateX(o.scroll.y),o.wheelTo(o.selectedIndex,0)}),this.scroll.hooks.on(this.scroll.hooks.eventTypes.beforeInitialScrollTo,function(e){e.x=0,e.y=-(o.selectedIndex*o.itemHeight)}),t.hooks.on(t.hooks.eventTypes.checkClick,function(){var e,e=(e=o.items,Array.prototype.slice.call(e,0).indexOf(o.target));return-1===e||o.wheelTo(e,o.options.adjustTime,d),!0}),t.hooks.on(t.hooks.eventTypes.scrollTo,function(e){e.y=o.findNearestValidWheel(e.y).y}),t.hooks.on(t.hooks.eventTypes.minDistanceScroll,function(){var e=t.animater;!0===e.forceStopped&&(e.forceStopped=!1)}),t.hooks.on(t.hooks.eventTypes.scrollToElement,function(e,t){return!c(e,o.options.wheelItemClass)||void(t.top=o.findNearestValidWheel(t.top).y)}),n.hooks.on(n.hooks.eventTypes.beforeStart,function(e){o.target=e.target}),s.hooks.on(s.hooks.eventTypes.computeBoundary,function(e){e.maxScrollPos=0,e.minScrollPos=0}),i.hooks.on(i.hooks.eventTypes.computeBoundary,function(e){o.items=o.scroll.scroller.content.children,o.checkWheelAllDisabled(),o.itemHeight=0<o.items.length?i.contentSize/o.items.length:0,e.maxScrollPos=-o.itemHeight*(o.items.length-1),e.minScrollPos=0}),i.hooks.on(i.hooks.eventTypes.momentum,function(e){e.rate=p,e.destination=o.findNearestValidWheel(e.destination).y}),i.hooks.on(i.hooks.eventTypes.end,function(e){var t=o.findNearestValidWheel(i.currentPos);e.destination=t.y,e.duration=o.options.adjustTime}),r.hooks.on(r.hooks.eventTypes.time,function(e){o.transitionDuration(e)}),r.hooks.on(r.hooks.eventTypes.timeFunction,function(e){o.timeFunction(e)}),r.hooks.on(r.hooks.eventTypes.callStop,function(){var e=o.findNearestValidWheel(o.scroll.y).index;o.isAdjustingPosition=!0,o.wheelTo(e,0)}),r.translater.hooks.on(r.translater.hooks.eventTypes.translate,function(e){o.rotateX(e.y)})},m.prototype.refreshBoundary=function(){var e=this.scroll.scroller,t=e.scrollBehaviorX,o=e.scrollBehaviorY,e=e.content;t.refresh(e),o.refresh(e)},m.prototype.setSelectedIndex=function(e,t){void 0===t&&(t=!1),this.selectedIndex===(this.selectedIndex=e)||t||this.scroll.trigger(f,e)},m.prototype.getSelectedIndex=function(){return this.selectedIndex},m.prototype.wheelTo=function(e,t,o){e=-(e=void 0===e?0:e)*this.itemHeight;this.scroll.scrollTo(0,e,t=void 0===t?0:t,o)},m.prototype.restorePosition=function(){var e;this.scroll.pending&&(e=this.getSelectedIndex(),this.scroll.scroller.animater.clearTimer(),this.wheelTo(e,0))},m.prototype.transitionDuration=function(e){for(var t=0;t<this.items.length;t++)this.items[t].style[h]=e+"ms"},m.prototype.timeFunction=function(e){for(var t=0;t<this.items.length;t++)this.items[t].style[a]=e},m.prototype.rotateX=function(e){for(var t=this.options.rotate,o=void 0===t?25:t,n=0;n<this.items.length;n++){var s=(o*(e/this.itemHeight+n)).toFixed(3);this.items[n].style[l]="rotateX("+s+"deg)"}},m.prototype.findNearestValidWheel=function(e){e=0<e?0:e<this.scroll.maxScrollY?this.scroll.maxScrollY:e;for(var t=Math.abs(Math.round(-e/this.itemHeight)),e=t,o=this.items,n=this.options.wheelDisabledItemClass;0<=t&&c(o[t],n);)t--;if(t<0)for(t=e;t<=o.length-1&&c(o[t],n);)t++;return t===o.length&&(t=e),{index:this.wheelItemsAllDisabled?-1:t,y:-t*this.itemHeight}},m.prototype.checkWheelAllDisabled=function(){var e=this.options.wheelDisabledItemClass,t=this.items;this.wheelItemsAllDisabled=!0;for(var o=0;o<t.length;o++)if(!c(t[o],e)){this.wheelItemsAllDisabled=!1;break}},m.pluginName="wheel",m});