forked from beeplin/vue-keep-scroll-position
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (42 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(function () {
var plugin;
plugin = function (Vue) {
return Vue.directive('keep-scroll-position', {
bind: function (el) {
el.addEventListener('scroll', function (event) {
event.target.setAttribute('data-vue-keep-scroll-position', event.target.scrollLeft + '-' + event.target.scrollTop);
});
},
inserted: function (el) {
var i, len, pos, target, targets;
targets = el.querySelectorAll('[data-vue-keep-scroll-position]');
if (targets.length > 0) {
for (i = 0, len = targets.length; i < len; i++) {
target = targets[i];
pos = target.getAttribute('data-vue-keep-scroll-position').split('-');
target.scrollLeft = pos[0];
target.scrollTop = pos[1];
}
} else {
if (el.hasAttribute('data-vue-keep-scroll-position')) {
pos = el.getAttribute('data-vue-keep-scroll-position').split('-');
el.scrollLeft = pos[0];
el.scrollTop = pos[1];
}
}
}
});
};
if (typeof Vue !== "undefined" && Vue !== null) {
Vue.use(plugin);
}
if (typeof exports === 'object' && typeof module === 'object') {
return module.exports = plugin;
} else if (typeof define === 'function' && define.amd) {
return define(function () {
return plugin;
});
} else if (typeof window !== "undefined" && window !== null) {
return window.VueKeepScrollPosition = plugin;
}
})();