-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindNodeAutoScroll.js
58 lines (43 loc) · 1.22 KB
/
bindNodeAutoScroll.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
//#!py
function bindNodeAutoScroll(node, op)
op = Object.assign({
offset: 20,
duration: .5,
interval: 3,
delay: 0,
}, op)
op.timer = setTimeout(scroll, op.delay * 1000)
op.stop = ()=>
clearTimeout(op.timer)
op.timer = null
op.start = ()=>
scroll()
return op
function scroll()
op.timer = setTimeout(()=> {
const height = node.scrollHeight - node.clientHeight
if height <= 0
return
if node.scrollTop >= height
scrollTo(-height, op.duration, scroll)
else
const targetOffset = op.offset === 'page' ? node.clientHeight : op.offset
scrollTo(targetOffset, op.duration, scroll)
}, op.interval * 1000)
function scrollTo(targetOffset, duration, cb)
const sign = targetOffset / Math.abs(targetOffset)
const offsetValue = Math.abs(targetOffset)
const animateInterval = duration * 1000 / 24
const step = offsetValue / 24
// Math.max((targetOffset / step), 30)
// console.log(animateInterval, step)
const scrollTop = node.scrollTop
let offset = 0
const timer = setInterval(()=> {
if offset >= offsetValue
clearInterval(timer)
cb()
else
offset += step
node.scrollTop = scrollTop + offset * sign
}, animateInterval)