-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstickyScroll.js
74 lines (61 loc) · 1.98 KB
/
stickyScroll.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
(function() {
// if we are in node.js enviro, require vue
try {
var Vue = require('vue');
} catch (e) {
// no worries, in browser enviro Vue should already be global
}
var vueStickyScroll = Vue.directive('sticky-scroll', {
bind: function() {
//use browser MutationObserver object
var observer = new MutationObserver(scrollToBottom);
//looking for new children that will change the height
var config = { childList: true };
observer.observe(this.el, config);
//need reference to this, otherwise 'this'=MutationObserver
var me = this;
function animateScroll(duration) {
var start = me.el.scrollTop;
var end = me.el.scrollHeight;
var change = end - start;
var increment = 20;
function easeInOut(currentTime, start, change, duration) {
//by Robert Penner
currentTime /= duration / 2;
if (currentTime < 1) {
return change / 2 * currentTime * currentTime + start;
}
currentTime -= 1;
return -change / 2 * (currentTime * (currentTime - 2) - 1) + start;
}
function animate(elapsedTime) {
elapsedTime += increment;
var position = easeInOut(elapsedTime, start, change, duration);
me.el.scrollTop = position;
if (elapsedTime < duration) {
setTimeout(function() {
animate(elapsedTime);
}, increment)
}
}
animate(0);
}
function scrollToBottom() {
if (me.arg === 'animate') {
//default is 300
var duration = Number(me.expression) || 300;
animateScroll(duration);
} else {
//default is jump to bottom
me.el.scrollTop = me.el.scrollHeight;
}
}
}
});
// check whether we are in node.js enviro
try {
module.exports = vueStickyScroll;
} catch (e) {
// no worries, our directive will just be registered in browser
}
})();