-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrolling.html
242 lines (236 loc) · 7.29 KB
/
scrolling.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js"></script>
<style>
body, ul {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.wrapper {
position: absolute;
top: 50%;
left: 0;
right: 0;
margin: 0 auto;
height: 80%;
width: 80%;
max-width: 300px;
max-height: 500px;
border: 1px solid #000;
transform: translateY(-50%);
overflow: hidden;
}
.list {
background-color: #70f3b7;
}
.list-item {
height: 40px;
line-height: 40px;
width: 100%;
text-align: center;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="app"></div>
<template id="tpl">
<div
class="wrapper"
ref="wrapper"
@touchstart.prevent="onStart"
@touchmove.prevent="onMove"
@touchend.prevent="onEnd"
@touchcancel.prevent="onEnd"
@mousedown.prevent="onStart"
@mousemove.prevent="onMove"
@mouseup.prevent="onEnd"
@mousecancel.prevent="onEnd"
@mouseleave.prevent="onEnd"
@transitionend="onTransitionEnd"
>
<ul
class="list"
ref="scroller"
:style="scrollerStyle"
>
<li
class="list-item"
v-for="item in list"
>
{{item}}
</li>
</ul>
</div>
</template>
<script>
new Vue({
el: '#app',
template: '#tpl',
computed: {
list() {
const list = []
for (let i = 0; i < 100; i++) {
list.push(i)
}
return list
},
scrollerStyle() {
return {
'transform': `translate3d(0, ${this.offsetY}px, 0)`,
'transition-duration': `${this.duration}ms`,
'transition-timing-function': this.bezier,
}
}
},
data() {
return {
wrapper: null,
scroller: null,
minY: 0,
maxY: 0,
wrapperHeight: 0,
offsetY: 0, // 元素实时 y 偏移值
duration: 0,
bezier: 'linear',
startY: 0, // touchStart 元素 y 偏移值
pointY: 0, // touchStart 手势 y 坐标
startTime: 0, // 惯性滑动范围内的 startTime
momentumStartY: 0, // 惯性滑动范围内的 startY
momentumTimeThreshold: 300, // 惯性滑动的启动 时间阈值
momentumYThreshold: 15, // 惯性滑动的启动 距离阈值
isStarted: false, // start锁
}
},
mounted() {
this.$nextTick(() => {
this.wrapper = this.$refs.wrapper
this.scroller = this.$refs.scroller
const { height: wrapperHeight } = this.wrapper.getBoundingClientRect()
const { height: scrollHeight } = this.scroller.getBoundingClientRect()
this.wrapperHeight = wrapperHeight
this.minY = wrapperHeight - scrollHeight
})
},
methods: {
onStart(e) {
const point = e.touches ? e.touches[0] : e
this.isStarted = true
this.duration = 0
this.stop()
this.pointY = point.pageY
this.momentumStartY = this.startY = this.offsetY
this.startTime = new Date().getTime()
},
onMove(e) {
if (!this.isStarted) return
const point = e.touches ? e.touches[0] : e
const deltaY = point.pageY - this.pointY
// 浮点数坐标会影响渲染速度
let offsetY = Math.round(this.startY + deltaY)
// 超出边界时增加阻力
if (offsetY < this.minY || offsetY > this.maxY) {
offsetY = Math.round(this.startY + deltaY / 3)
}
this.offsetY = offsetY
console.log('offsetY', this.offsetY)
const now = new Date().getTime()
// 记录在触发惯性滑动条件下的偏移值和时间
if (now - this.startTime > this.momentumTimeThreshold) {
this.momentumStartY = this.offsetY
this.startTime = now
}
},
onEnd(e) {
if (!this.isStarted) return
this.isStarted = false
if (this.isNeedReset()) return
const absDeltaY = Math.abs(this.offsetY - this.momentumStartY)
const duration = new Date().getTime() - this.startTime
console.log('absDeltaY', absDeltaY)
console.log('duration', duration)
console.log('speed', absDeltaY / duration)
// 启动惯性滑动
if (duration < this.momentumTimeThreshold && absDeltaY > this.momentumYThreshold) {
const momentum = this.momentum(this.offsetY, this.momentumStartY, duration)
this.offsetY = Math.round(momentum.destination)
this.duration = momentum.duration
this.bezier = momentum.bezier
}
},
onTransitionEnd() {
this.isNeedReset()
},
momentum(current, start, duration) {
const durationMap = {
'noBounce': 2500,
'weekBounce': 800,
'strongBounce': 400,
}
const bezierMap = {
'noBounce': 'cubic-bezier(.17, .89, .45, 1)',
'weekBounce': 'cubic-bezier(.25, .46, .45, .94)',
'strongBounce': 'cubic-bezier(.25, .46, .45, .94)',
}
let type = 'noBounce'
// 惯性滑动加速度
const deceleration = 0.003
// 回弹阻力
const bounceRate = 10
// 强弱回弹的分割值
const bounceThreshold = 300
// 回弹的最大限度
const maxOverflowY = this.wrapperHeight / 6
let overflowY
const distance = current - start
const speed = 2 * Math.abs(distance) / duration
let destination = current + speed / deceleration * (distance < 0 ? -1 : 1)
if (destination < this.minY) {
overflowY = this.minY - destination
type = overflowY > bounceThreshold ? 'strongBounce' : 'weekBounce'
destination = Math.max(this.minY - maxOverflowY, this.minY - overflowY / bounceRate)
} else if (destination > this.maxY) {
overflowY = destination - this.maxY
type = overflowY > bounceThreshold ? 'strongBounce' : 'weekBounce'
destination = Math.min(this.maxY + maxOverflowY, this.maxY + overflowY / bounceRate)
}
return {
destination,
duration: durationMap[type],
bezier: bezierMap[type],
}
},
// 超出边界时需要重置位置
isNeedReset() {
let offsetY
if (this.offsetY < this.minY) {
offsetY = this.minY
} else if (this.offsetY > this.maxY) {
offsetY = this.maxY
}
if (typeof offsetY !== 'undefined') {
this.offsetY = offsetY
this.duration = 500
this.bezier = 'cubic-bezier(.165, .84, .44, 1)'
return true
}
return false
},
stop() {
// 获取当前 translate 的位置
const matrix = window.getComputedStyle(this.scroller).getPropertyValue('transform')
this.offsetY = Math.round(+matrix.split(')')[0].split(', ')[5])
}
}
})
</script>
</body>
</html>