-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginator.js
246 lines (196 loc) · 6.43 KB
/
paginator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { addScript } from '/parent/core/dom.js'
import { debounce } from './utility.js'
import * as a from '/parent/core/animate.js'
class Paginator extends HTMLElement {
constructor() {
super()
this.tween = {}
this.resize = this.resize.bind(this)
this.wheel = this.wheel.bind(this)
this.wheelEnd = debounce(500, this.jumpToEdge.bind(this))
this.keydown = this.keydown.bind(this)
this.scrollUpdate = this.scrollUpdate.bind(this)
}
connectedCallback() {
window.addEventListener('wheel', this.wheel)
window.addEventListener('keydown', this.keydown)
window.addEventListener('resize', this.resize)
// setTimeout because the article is restoring the scroll
setTimeout(() => this.scrollUpdate())
}
disconnectedCallback() {
window.removeEventListener('wheel', this.wheel)
window.removeEventListener('keydown', this.keydown)
window.removeEventListener('resize', this.resize)
}
// the one that's underneath if there are two
// previousPage() {
// return Array.from(this.children).find(el => {
// const rect = el.getBoundingClientRect()
// return rect.top <= 0 && rect.bottom >= 0
// })
// }
// the one on the bottom of the screen, even if by one pixel
currentPage() {
return Array.from(this.children).find(el =>
el.getBoundingClientRect().bottom >= window.innerHeight
)
}
tweenScroll(duration, delta) {
// console.log('tweenScroll', duration, delta)
const scroll = this.tween.active ? this.tween.endValue : window.scrollY
if (this.tween && this.tween.active) this.tween.cancel()
const ease = a.cubicOut(scroll, scroll + delta)
this.tween = a.timer({
duration,
endValue: scroll + delta,
tick: time => {
window.scrollTo(0, ease(time))
this.scrollUpdate()
}
})
}
bounceScroll(amount) {
const scroll = this.tween.active ? this.tween.endValue : window.scrollY
if (this.tween && this.tween.active) this.tween.cancel()
let ease = a.cubicOut(scroll, scroll + amount)
// first duration could be: scrollMax == scrollHeight ? 80 : 300,
let duration = 80
let last = this.children[ this.children.length - 1 ]
const scrollMax = document.body.scrollHeight - window.innerHeight
const tick = time => {
window.scrollTo(0, ease(time))
let y = Math.round(Math.min(0, scrollMax - ease(time)))
last.style.transform = `translateY(${ y }px)`
}
this.tween = a.timer({
tick,
duration,
endValue: scroll + amount,
done: () => {
ease = a.cubicOut(scroll + amount, scroll)
this.tween = a.timer({ tick, duration, endValue: scroll })
}
})
}
resize(e) {
this.scrollUpdate()
this.jumpToEdge()
}
keydown(e) {
if (e.defaultPrevented) return
const page = this.currentPage()
const rect = page && page.getBoundingClientRect()
const scroll = this.tween.active ? this.tween.endValue : window.scrollY
// console.log(e.key) // , rect)
switch (e.key) {
case "ArrowDown":
e.preventDefault()
const scrollMax = document.body.scrollHeight - window.innerHeight
// finish a transition
if (Math.floor(rect.top) > 0)
this.tweenScroll(600, rect.top)
// move as normal down arrow
else if (Math.floor(rect.bottom) > window.innerHeight)
this.tweenScroll(100, Math.min(rect.bottom - window.innerHeight, 40))
// bounce scroll
else if (scroll >= scrollMax)
this.bounceScroll(80)
// transition to next page
else
this.tweenScroll(400, window.innerHeight)
break
case "ArrowUp":
e.preventDefault()
if (Math.floor(rect.top) > 0)
this.tweenScroll(600, -rect.top)
else if (Math.floor(rect.top) < 0)
this.tweenScroll(100, Math.max(rect.top, -40))
else if (scroll <= 0)
this.bounceScroll(-80)
else
this.tweenScroll(400, -window.innerHeight)
break
case "PageDown":
e.preventDefault()
break
case "PageUp":
e.preventDefault()
break
case " ":
e.preventDefault()
if (e.shiftKey)
console.log('shift space')
break
}
}
wheel(e) {
if (this.tween && this.tween.active) this.tween.cancel()
e.preventDefault()
window.scrollBy(0, e.deltaY)
this.scrollUpdate()
this.wheelEnd()
}
jumpToEdge(e) {
if (this.tween.active) return
const page = this.currentPage()
if (!page) return
const duration = 600
const rect = page.getBoundingClientRect()
const tolerance = window.innerHeight * .2
if (window.innerHeight - rect.top < tolerance) {
const endValue = window.scrollY - (window.innerHeight - rect.top)
const ease = a.cubicInOut(window.scrollY, endValue)
this.tween = a.timer({
duration,
endValue,
tick: time => {
window.scrollTo(0, ease(time))
this.scrollUpdate()
}
})
}
else if (rect.top < tolerance) {
const endValue = window.scrollY + rect.top
const ease = a.cubicInOut(window.scrollY, endValue)
this.tween = a.timer({
duration,
endValue,
tick: time => {
window.scrollTo(0, ease(time))
this.scrollUpdate()
}
})
}
}
scrollUpdate() {
const children = Array.from(this.children)
const child = children.find(el => {
const match = el.style.transform.match(/translateY\((-?\d*)px\)/)
const y = match ? match[1] * 1 : 0
const rect = el.getBoundingClientRect()
return rect.top - y <= 0 && rect.bottom - y >= 0
})
if (child) {
const rect = child.getBoundingClientRect()
const match = child.style.transform.match(/translateY\((-?\d*)px\)/)
const y = match ? match[1] * 1 : 0
const val = -1 * (rect.bottom - window.innerHeight - y)
const pct = val / window.innerHeight
if (val > 0) {
const y = Math.round(val - a.circOut(0, window.innerHeight * .1, pct))
child.style.transform = `translateY(${ y }px)`
child.style.opacity = a.quintIn(1, 0, pct)
}
else {
child.style.transform = 'translate3d(0,0,0)'
child.style.opacity = 1
}
}
children.filter(el => el != child).forEach(el => {
el.style.transform = 'translate3d(0,0,0)'
el.style.opacity = 1
})
}
}
window.customElements.define('fx-paginator', Paginator)