vue-scroll
Version:
scroll directive for vuejs 2.0
6 lines • 2.74 kB
JavaScript
/**
* vue-scroll vundefined
* (c) 2019 Wang Pin
* @license MIT
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).VueScroll=t()}(this,function(){"use strict";var debounce=function(n,o){var r;return function(){var e=this,t=arguments;clearTimeout(r),r=setTimeout(function(){return n.apply(e,t)},o)}},throttle=function(n,o){var r,i;return function(){var e=this,t=arguments;i?(clearTimeout(r),r=setTimeout(function(){Date.now()-i>=o&&(n.apply(e,t),i=Date.now())},o-(Date.now()-i))):(n.apply(e,t),i=Date.now())}},isNumber=function(e){return"number"==typeof e&&NaN!==e},isFunction=function(e){return"function"==typeof e},isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)},isInteger=function(e){return isNumber(e)&&Math.round(e)===e},get=function(arg,path,def){try{return eval("arg."+path)}catch(e){return def}},dom=(s=new Map,t="scroll",{bind:function(e,n,o,r){var i,c;if(!isFunction(o))throw new Error("Scroll handler is not a function");s.has(e)||s.set(e,new Map),(i=s.get(e)).has(n)||i.set(n,[]),(c=i.get(n)).length||function(e,n,r,o){function i(n){var o,e=n.target||n.srcElement;(n=n||window.e).type===t&&(o=e===document?{scrollTop:get(document,"body.scrollTop",0),scrollLeft:get(document,"body.scrollLeft",0)}:{scrollTop:get(e,"scrollTop",0),scrollLeft:get(e,"scrollLeft",0)}),r.forEach(function(e){e(n,o)})}isObject(o)&&(isInteger(o.throttle)&&isFinite(o.throttle)&&-1<o.throttle&&(i=throttle(i,o.throttle)),isInteger(o.debounce)&&isFinite(o.debounce)&&-1<o.debounce&&(i=debounce(i,o.debounce))),n===t&&(e===document.body||e===document||e===window?document.onscroll=i:e.addEventListener?e.addEventListener(n,i):e.attachEvent("on"+n,i))}(e,n,c,r),c.push(o)},unbind:function(e,t,n){var o,r;if(isFunction(n))return s.has(e)||s.set(e,new Map),(o=s.get(e)).has(t)||o.set(t,[]),-1<(r=o.get(t)).indexOf(n)&&(r.splice(r.indexOf(n),1),!0)}}),s,t,vuescroll=new Object;return vuescroll.install=function(e,i){i=i||{};var c="scroll",u="throttle",l="debounce",s=[u,l];function r(e,t,n){var o,r=Object.assign({},i);if(isObject(t)||isFunction(t)){o=t,-1<s.indexOf(n)&&(o=t.fn,n===u?r={throttle:t.throttle}:n===l&&(r={debounce:t.debounce}));try{dom.bind(e,c,o,r)}catch(e){console.warn("Unexpected error happened when binding listener")}}else console.warn("Unexpected scroll properties")}function n(e,t,n){var o;(isObject(t)||isFunction(t))&&(o=t,-1<s.indexOf(n)&&(o=t.fn),dom.unbind(e,c,o))}e.directive(c,{bind:function(e,t,n,o){r(e,t.value,t.arg)},inserted:function(e,t){},update:function(e,t){t.value!==t.oldValue&&(r(e,t.value,t.arg),n(e,t.oldValue,t.arg))},unbind:function(e,t){n(e,t.value,t.arg)}})},vuescroll});