-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathmpx-movable-view.vue
More file actions
492 lines (476 loc) · 14.8 KB
/
mpx-movable-view.vue
File metadata and controls
492 lines (476 loc) · 14.8 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<template>
<div class="mpx-movable-scroll-content" ref="scrollContent">
<div class="mpx-movable-scroll-item">
<slot></slot>
</div>
</div>
</template>
<script type="text/ecmascript-6">
import { getCustomEvent, extendEvent } from './getInnerListeners'
import { extend } from '../../utils'
import BScroll from '@better-scroll/core'
import Movable from '@better-scroll/movable'
import ObserveDOM from '@better-scroll/observe-dom'
import Zoom from '@better-scroll/zoom'
BScroll.use(Movable)
BScroll.use(Zoom)
BScroll.use(ObserveDOM)
export default {
data () {
return {
directions: ['none', 'all', 'vertical', 'horizontal'],
minScrollX: 0,
maxScrollX: 0,
minScrollY: 0,
maxScrollY: 0,
currentX: this.x,
currentY: this.y,
lastestX: 0,
lastestY: 0,
lastestScale: 1,
bsOptions: {},
isZooming: false,
isFirstTouch: true,
source: '',
touchEvent: '',
isInited: false,
deactivatedX: 0,
deactivatedY: 0,
// 缓存高度,用于检测变化
cachedContentHeight: 0,
cachedWrapperHeight: 0,
// 缓存宽度,用于检测变化
cachedContentWidth: 0,
cachedWrapperWidth: 0,
positionSyncPending: false
}
},
props: {
direction: {
type: String,
default: 'none'
},
inertia: {
type: Boolean,
default: false
},
outOfBounds: {
type: Boolean,
default: false
},
x: {
type: Number,
default: 0
},
y: {
type: Number,
default: 0
},
damping: {
type: Number,
default: 20
},
friction: {
type: Number,
default: 2
},
disabled: {
type: Boolean,
default: false
},
scale: {
type: Boolean,
default: false
},
scaleMin: {
type: Number,
default: 0.5
},
scaleMax: {
type: Number,
default: 10
},
scaleValue: {
type: Number,
default: 1
},
animation: {
type: Boolean,
default: true
},
speed: {
type: Number,
default: 500
},
scrollOptions: {
type: Object,
default: () => {
return {}
}
},
},
watch: {
x () {
if (this.direction === 'vertical' || this.direction === 'none') {
return
}
this.source = ''
this.scheduleSyncPosition()
},
y () {
if (this.direction === 'horizontal' || this.direction === 'none') {
return
}
this.source = ''
this.scheduleSyncPosition()
},
scaleValue (newVal) {
this.isZooming = true
if (newVal > 10) {
newVal = 10
}
if (newVal < 0.5) {
newVal = 0.5
}
this.bs.zoomTo(newVal, 'center', 'center')
},
disabled () {
this.init()
}
},
mounted () {
if (!this.scrollOptions.closeResizeObserver) {
this.createResizeObserver()
}
// 初始化尺寸缓存
this.$nextTick(() => {
this.initSizeCache()
})
this.init()
},
activated () {
if (this.deactivatedX || this.deactivatedY) {
this.refresh()
this.bs.putAt(this.deactivatedX, this.deactivatedY, 0)
}
},
deactivated () {
// when the hook is triggered
// bs will recalculate the boundary of movable to 0
// so record the position of the movable
this.deactivatedX = this.bs.x
this.deactivatedY = this.bs.y
},
beforeDestroy () {
this.destroyBs()
if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
},
methods: {
createResizeObserver () {
if (typeof ResizeObserver !== 'undefined') {
this.resizeObserver = new ResizeObserver(entries => {
if (!this.isInited) {
this.isInited = true
return
}
this.refresh()
})
this.resizeObserver.observe(this.$refs.scrollContent)
}
},
refresh () {
this.bs && this.bs.refresh()
},
scheduleSyncPosition () {
if (!this.bs) return
if (this.positionSyncPending) return
this.positionSyncPending = true
this.$nextTick(() => {
this.positionSyncPending = false
this.syncPositionFromProps()
})
},
syncPositionFromProps () {
if (!this.bs) return
const widthChanged = this.checkWidthChange()
const heightChanged = this.checkHeightChange()
const currentX = this.bs.x
const currentY = this.bs.y
if (widthChanged || heightChanged) {
// 兼容容器尺寸变化且同时改变x/y的场景,ResizeObserver回调是异步的,
// 如果不直接refresh,minScrollX/maxScrollX/minScrollY/maxScrollY 拿到的都是上一次的值
this.refresh()
// bs refresh 方法内会触发 resetPosition(),如果容器宽高收缩从100 - 50,会导致位置立即跳转到边界内,
// 没有动画效果,造成视觉突兀;如果 refresh 导致了位置变化,先恢复到原位置再执行目标滚动
// 刷新边界后保持当前视觉位置,再执行目标滚动,避免突然跳变
if (this.bs.x !== currentX || this.bs.y !== currentY) {
this.bs.scrollTo(currentX, currentY, 0)
}
}
let targetX = this.x
let targetY = this.y
if (targetX > this.bs.minScrollX) {
targetX = this.bs.minScrollX
}
if (targetX < this.bs.maxScrollX) {
targetX = this.bs.maxScrollX
}
if (targetY > this.bs.minScrollY) {
targetY = this.bs.minScrollY
}
if (targetY < this.bs.maxScrollY) {
targetY = this.bs.maxScrollY
}
this.currentX = targetX
this.currentY = targetY
this.bs.scrollTo(targetX, targetY, this.speed)
},
// 检查高度是否发生变化
checkHeightChange () {
if (!this.$refs.scrollContent || !this.$parent.$refs.movableArea) {
return false
}
const currentContentHeight = this.$refs.scrollContent.clientHeight
const currentWrapperHeight = this.$parent.$refs.movableArea.clientHeight
const heightChanged =
currentContentHeight !== this.cachedContentHeight ||
currentWrapperHeight !== this.cachedWrapperHeight
// 更新缓存的高度
this.cachedContentHeight = currentContentHeight
this.cachedWrapperHeight = currentWrapperHeight
return heightChanged
},
// 检查宽度是否发生变化
checkWidthChange () {
if (!this.$refs.scrollContent || !this.$parent.$refs.movableArea) {
return false
}
const currentContentWidth = this.$refs.scrollContent.clientWidth
const currentWrapperWidth = this.$parent.$refs.movableArea.clientWidth
const widthChanged =
currentContentWidth !== this.cachedContentWidth ||
currentWrapperWidth !== this.cachedWrapperWidth
// 更新缓存的宽度
this.cachedContentWidth = currentContentWidth
this.cachedWrapperWidth = currentWrapperWidth
return widthChanged
},
destroyBs () {
if (!this.bs) return
this.bs.destroy()
delete this.bs
},
init () {
this.destroyBs()
if (!this.$refs.scrollContent.parentNode || (this.$refs.scrollContent.parentNode && this.$refs.scrollContent.parentNode.className !== 'mpx-movable-scroll-wrapper')) {
return
}
this.initOptions()
const el = this.$refs.scrollContent
this.bs = new BScroll(this.$parent.$refs.scroll, extend({
specifiedIndexAsContent: [].indexOf.call(el.parentElement.children, el) || 0,
bindToTarget: true,
freeScroll: false,
scrollX: false,
scrollY: false,
movable: true,
startX: this.currentX,
startY: this.currentY,
bounce: this.outOfBounds,
bounceTime: 800 / (this.damping / 20),
probeType: 3
}, this.bsOptions))
const BehaviorHooks = this.bs.scroller.scrollBehaviorY.hooks
const actionsHandlerHooks = this.bs.scroller.actionsHandler.hooks
const scrollerHooks = this.bs.scroller.hooks
this.bs.putAt(this.currentX, this.currentY, 0)
this.lastestX = this.roundFun(this.x)
this.lastestY = this.roundFun(this.y)
this.lastestScale = this.roundFun(this.scaleValue)
this.minScrollX = this.bs.minScrollX
this.maxScrollX = this.bs.maxScrollX
this.minScrollY = this.bs.minScrollY
this.maxScrollY = this.bs.maxScrollY
scrollerHooks.on('beforeScrollStart', (position) => {
this.source = 'touch'
})
scrollerHooks.on('scroll', (position) => {
if (position.x > this.minScrollX && this.bs.movingDirectionX === -1 ||
(position.x < this.maxScrollX && this.bs.movingDirectionX === 1) ||
(position.y > this.minScrollY && this.bs.movingDirectionY === -1) ||
(position.y < this.maxScrollY && this.bs.movingDirectionY === 1)) {
this.source = 'touch-out-of-bounds'
}
if (this.direction !== 'none' && (this.directions.indexOf(this.direction) >= 0)) {
if (this.isZooming || (this.roundFun(position.x) === this.lastestX && this.roundFun(position.y) === this.lastestY)) {
return
}
this.$emit('change', getCustomEvent('change', {
x: this.roundFun(position.x) ? this.roundFun(position.x) : 0,
y: this.roundFun(position.y) ? this.roundFun(position.y) : 0,
source: this.source
}, this))
}
this.lastestX = this.roundFun(position.x)
this.lastestY = this.roundFun(position.y)
})
scrollerHooks.on('scrollEnd', (position) => {
this.currentX = this.bs.x
this.currentY = this.bs.y
if (this.direction !== 'none' && (this.directions.indexOf(this.direction) >= 0)) {
const endX = this.roundFun(position && typeof position.x === 'number' ? position.x : this.bs.x)
const endY = this.roundFun(position && typeof position.y === 'number' ? position.y : this.bs.y)
// 只在最终位置与最近一次 change 不一致时补发,避免重复事件
if (!this.isZooming && (endX !== this.lastestX || endY !== this.lastestY)) {
this.$emit('change', getCustomEvent('change', {
x: endX ? endX : 0,
y: endY ? endY : 0,
source: this.source
}, this))
this.lastestX = endX
this.lastestY = endY
}
}
})
scrollerHooks.on('touchEnd', (position) => {
this.isFirstTouch = true
if (position.x > this.minScrollX || position.x < this.maxScrollX ||
position.y > this.minScrollY || position.y < this.maxScrollY
) {
this.source = 'out-of-bounds'
}
if (position.x > this.minScrollX) {
this.bs.movingDirectionX = 1
}
if (position.x < this.maxScrollX) {
this.bs.movingDirectionX = -1
}
if (position.y > this.minScrollY) {
this.bs.movingDirectionY = 1
}
if (position.y < this.maxScrollY) {
this.bs.movingDirectionY = -1
}
})
actionsHandlerHooks.on('start', (e) => {
extendEvent(e, { detail: Object.assign({}, e.detail) })
this.$emit('touchstart', e)
})
actionsHandlerHooks.on('end', (e) => {
extendEvent(e, { detail: Object.assign({}, e.detail) })
this.$emit('touchend', e)
})
actionsHandlerHooks.on('move', ({ deltaX, deltaY, e }) => {
if (this.isZooming) {
return
}
if (this.isFirstTouch) {
if (Math.abs(deltaX) - Math.abs(deltaY) > 0) {
this.touchEvent = 'htouchmove'
} else {
this.touchEvent = 'vtouchmove'
}
}
extendEvent(e, { detail: Object.assign({}, e.detail), currentTarget: e.target })
this.$emit(this.touchEvent, e)
this.$emit('touchmove', e)
this.isFirstTouch = false
})
if (this.inertia) { // movable-view是否带有惯性
BehaviorHooks.on('momentum', (momentumData, distance) => {
this.source = 'friction'
})
}
if (this.scale) { // 支持双指缩放
this.bs.on('zooming', ({ scale }) => {
if (this.lastestScale === this.roundFun(scale)) {
return
}
this.isZooming = true
this.$emit('scale', getCustomEvent('change', {
x: this.roundFun(this.bs.x),
y: this.roundFun(this.bs.y),
scale: this.roundFun(scale)
}, this))
this.lastestScale = this.roundFun(scale)
})
this.bs.on('zoomEnd', ({ scale }) => {
this.isZooming = false
})
}
},
initOptions () {
if (!this.friction || this.friction < 0) {
this.friction = 2
}
if (this.$parent.$attrs && this.$parent.$attrs['scale-area'] === "true") {
extend(this.bsOptions, {
bindToTarget: !this.scale
})
}
if (this.scale) {
extend(this.bsOptions, {
zoom: { // for zoom plugin
start: this.scaleValue,
min: this.scaleMin < 0.5 ? 0.5 : this.scaleMin,
max: this.scaleMax > 10 ? 10 : this.scaleMax
}
})
}
if (this.inertia) {
extend(this.bsOptions, {
momentum: true,
momentumLimitDistance: 30,
deceleration: this.friction / 2 * 0.05,
swipeTime: 50
})
}
if (this.disabled) {
extend(this.bsOptions, {
freeScroll: false,
scrollY: false,
scrollX: false
})
} else if (this.direction === 'vertical') {
extend(this.bsOptions, {
scrollY: true
})
} else if (this.direction === 'horizontal') {
extend(this.bsOptions, {
scrollX: true
})
} else if (this.direction === 'all') {
extend(this.bsOptions, {
freeScroll: true,
scrollX: true,
scrollY: true
})
}
extend(this.bsOptions, this.scrollOptions)
},
// 初始化尺寸缓存
initSizeCache () {
if (this.$refs.scrollContent && this.$parent.$refs.movableArea) {
this.cachedContentHeight = this.$refs.scrollContent.clientHeight
this.cachedWrapperHeight = this.$parent.$refs.movableArea.clientHeight
this.cachedContentWidth = this.$refs.scrollContent.clientWidth
this.cachedWrapperWidth = this.$parent.$refs.movableArea.clientWidth
}
},
// 处理小数点,四舍五入,默认保留一位小数
roundFun (value, n = 1) {
return Math.round(value * Math.pow(10, n)) / Math.pow(10, n)
}
}
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.mpx-movable-scroll-content
position: absolute
.mpx-movable-scroll-item
width: 100%
height: 100%
</style>