UNPKG

4.24 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.MugenScroll = factory());
5}(this, (function () { 'use strict';
6
7var index = throttle;
8
9/**
10 * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds.
11 *
12 * @param {Function} func Function to wrap.
13 * @param {Number} wait Number of milliseconds that must elapse between `func` invocations.
14 * @return {Function} A new function that wraps the `func` function passed in.
15 */
16
17function throttle (func, wait) {
18 var ctx, args, rtn, timeoutID; // caching
19 var last = 0;
20
21 return function throttled () {
22 ctx = this;
23 args = arguments;
24 var delta = new Date() - last;
25 if (!timeoutID)
26 { if (delta >= wait) { call(); }
27 else { timeoutID = setTimeout(call, wait - delta); } }
28 return rtn;
29 };
30
31 function call () {
32 timeoutID = 0;
33 last = +new Date();
34 rtn = func.apply(ctx, args);
35 ctx = null;
36 args = null;
37 }
38}
39
40var index$1 = function (element, ref) {
41 if ( ref === void 0 ) { ref = {}; }
42 var offset = ref.offset; if ( offset === void 0 ) { offset = 0; }
43 var threshold = ref.threshold; if ( threshold === void 0 ) { threshold = 0; }
44
45 var ref$1 = element.getBoundingClientRect();
46 var top = ref$1.top;
47 var right = ref$1.right;
48 var bottom = ref$1.bottom;
49 var left = ref$1.left;
50 var width = ref$1.width;
51 var height = ref$1.height;
52
53 var intersection = {
54 t: bottom,
55 r: window.innerWidth - left,
56 b: window.innerHeight - top,
57 l: right
58 };
59
60 var elementThreshold = {
61 x: threshold * width,
62 y: threshold * height
63 };
64
65 return (
66 intersection.t >= (offset.top || offset + elementThreshold.y) &&
67 intersection.r >= (offset.right || offset + elementThreshold.x) &&
68 intersection.b >= (offset.bottom || offset + elementThreshold.y) &&
69 intersection.l >= (offset.left || offset + elementThreshold.x)
70 )
71};
72
73var elementInView_common = index$1;
74
75var triggers = ['scroll', 'resize'];
76
77var MugenScroll = {
78 name: 'mugen-scroll',
79 props: {
80 handler: {
81 type: Function,
82 required: true
83 },
84 shouldHandle: {
85 type: Boolean,
86 default: true
87 },
88 threshold: {
89 type: Number,
90 default: 0
91 },
92 handleOnMount: {
93 type: Boolean,
94 default: true
95 },
96 scrollContainer: {
97 type: String
98 }
99 },
100 mounted: function mounted() {
101 this.checkInView();
102 },
103 methods: {
104 checkInView: function checkInView() {
105 var this$1 = this;
106
107 var execute = function () {
108 // The element can be removed
109 if (!this$1.$refs.scroll) {
110 return
111 }
112
113 var inView = elementInView_common(this$1.$refs.scroll, {
114 threshold: this$1.threshold
115 });
116 if (this$1.shouldHandle && inView) {
117 this$1.handler();
118 }
119 };
120
121 // checkInView right after this component is mounted
122 if (this.handleOnMount) {
123 execute();
124 }
125
126 if (this.scrollContainer) {
127 var parent = this;
128 while ((parent = parent.$parent) && !this._scrollContainer) {
129 this$1._scrollContainer = parent.$refs[this$1.scrollContainer];
130 }
131 // Ensure it's html element (ref could be component)
132 if (this._scrollContainer && this._scrollContainer.$el) {
133 this._scrollContainer = this._scrollContainer.$el;
134 }
135 }
136
137 this._scrollContainer = this._scrollContainer || window;
138
139 // Add event listeners
140 this.check = index(execute, 200);
141 triggers.forEach(function (event) { return this$1._scrollContainer.addEventListener(event, this$1.check); });
142 }
143 },
144 render: function render(h) {
145 return h('div', {
146 staticClass: 'mugen-scroll',
147 ref: 'scroll'
148 }, this.$slots.default)
149 },
150 beforeDestroy: function beforeDestroy() {
151 var this$1 = this;
152
153 triggers.forEach(function (event) { return this$1._scrollContainer.removeEventListener(event, this$1.check); });
154 }
155};
156
157if (typeof window !== 'undefined' && window.Vue) {
158 window.Vue.component(MugenScroll.name, MugenScroll);
159}
160
161return MugenScroll;
162
163})));