-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
78 lines (73 loc) · 2.86 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
; (function () {
var vueScrollspy = {};
vueScrollspy.install = function (Vue) {
Vue.directive('scrollspy', {
twoWay: true,
params: ["steps", "time", "offset"],
// {
// steps: {type: Number, required: false, default: 0},
// time: {type: Number, required: false, default: 0}
// },
scrollSections: [],
scroll: function(){
var offset = parseInt(this.params.offset) || 0
var pos = this.el.scrollTop + offset
var i = 0
while(pos >= this.scrollSections[i]){i++}
this.set(i ? i - 1 : 0)
},
scrollTo: function(id){
var current = this.el.scrollTop
var target = this.scrollSections[id]
var time = parseInt(this.params.time) || 0
var steps = parseInt(this.params.steps) || 0
var offset = parseInt(this.params.offset) || 0
var el = this.el
target -= offset
target < 0 ? 0 : target
if(!steps){
el.scrollTop = target
}else{
var timems = parseInt(time/steps)
var gap = target - current
for(var i = 0; i <= steps; i++){
(function(){
var pos = current + (gap/steps)*i;
setTimeout(function(){el.scrollTop = pos}, timems*i)
})();
}
}
},
init: function(){
this.scrollSections = []
var sections = this.el.children
for (var i = 0; i < sections.length; i++){
if(sections[i].offsetTop > 0){
this.scrollSections.push(sections[i].offsetTop)
}
}
},
bind: function(){
this.el.addEventListener('scroll', this.scroll.bind(this))
Vue.prototype.$scrollSet = this.init.bind(this)
Vue.prototype.$scrollTo = this.scrollTo.bind(this)
},
update: function(newVar, oldVar) {
// if(oldVar != newVar){
// this.scrollTo(newVar)
// }
},
unbind: function() {
this.el.removeEventListener('scroll', scroll)
},
})
}
if (typeof exports == "object") {
module.exports = vueScrollspy;
} else if (typeof define == "function" && define.amd) {
define([], function () { return vueScrollspy });
} else if (window.Vue) {
window.vueScrollspy = vueScrollspy;
Vue.use(vueScrollspy);
}
})();