/
index.js
173 lines (152 loc) · 4.81 KB
/
index.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
import {ScrollerCore, RequestAnimationFrameRender} from 'pure-logic-scroller'
const MOVE_START_EVENT = ['touchstart', 'mousedown']
const MOVE_EVENT = ['touchmove', 'mousemove']
const MOVE_END_EVENT = ['touchend', 'mouseup']
const MOVE_CANCEL_EVENT = ['touchcancel', 'mouseup']
function setElementTranslate(contentStyle, transformProp, x, y) {
contentStyle[transformProp] = `translate(${x}px, ${y}px)`
}
function standardizationEvent(event) {
if (window.TouchEvent&&event instanceof TouchEvent) {
event.clientX = event.changedTouches[0].clientX
event.clientY = event.changedTouches[0].clientY
}
}
class ScrollerDomHandler {
constructor() {
this.container = document.getElementById('container')
this.content = document.getElementById('content')
this.contentStyle = this.content.style
this.containerRect = null
this.contentRect = null
this.transformProp = 'transform'
this.events = [...MOVE_EVENT, ...MOVE_END_EVENT, ...MOVE_CANCEL_EVENT]
this.scrollerX = null
this.scrollerY = null
this.handleEvent = (e) => {
if (this.isPressDown && this.events.indexOf(e.type) === -1) {
return
}
if (MOVE_EVENT.indexOf(e.type) !== -1) {
this.handlePressMove(e)
} else if (MOVE_START_EVENT.indexOf(e.type) !== -1) {
this.handlePressStart(e)
} else if (MOVE_END_EVENT.indexOf(e.type) !== -1) {
this.handlePressEnd(e)
} else if (MOVE_CANCEL_EVENT.indexOf(e.type) !== -1) {
this.handlePressEnd(e)
}
}
this.testTransformProp()
this.computeRect()
this.bindEvent()
this.setScrollerCore()
}
setScrollerCore() {
let x = 0, y = 0
const moveContentX = (position) => {
x = position
setElementTranslate(this.contentStyle, this.transformProp, x, y)
}
const moveContentY = (position) => {
y = position
setElementTranslate(this.contentStyle, this.transformProp, x, y)
}
this.scrollerX = new ScrollerCore(
this.containerRect.width,
this.contentRect.width
)
this.scrollerX.useRender(new RequestAnimationFrameRender(moveContentX))
this.scrollerY = new ScrollerCore(
this.containerRect.height,
this.contentRect.height
)
this.scrollerY.useRender(new RequestAnimationFrameRender(moveContentY))
}
/**
* 测试浏览器支持的 css `transform` 属性名
*/
testTransformProp() {
if ('transform' in this.contentStyle) {
return
}
const prefix = ['webkit', 'ms', 'moz', 'o']
const prefixCount = prefix.length
for (let index = 0; index < prefixCount; index++) {
let transformProp = prefix[index] + 'Transform'
if (transformProp in this.contentStyle) {
this.transformProp = transformProp
return
}
}
}
computeRect() {
const containerStyle = getComputedStyle(this.container)
let width =
this.container.clientWidth -
containerStyle.paddingLeft.replace('px', '') -
containerStyle.paddingRight.replace('px', '')
let height =
this.container.clientHeight -
containerStyle.paddingTop.replace('px', '') -
containerStyle.paddingBottom.replace('px', '')
this.containerRect = {
width,
height,
}
this.contentRect = this.content.getBoundingClientRect()
}
bindEvent() {
this.events.forEach((event) => {
document.addEventListener(event, this.handleEvent)
})
MOVE_START_EVENT.forEach((event) => {
this.content.addEventListener(event, this.handleEvent)
})
}
removeEvent () {
this.events.forEach((event) => {
document.removeEventListener(event, this.handleEvent)
})
MOVE_START_EVENT.forEach((event) => {
this.content.removeEventListener(event, this.handleEvent)
})
}
handleEvent(e) {
if (this.events.indexOf(e.type) !== -1 && !this.isPressDown) {
return
}
if (MOVE_START_EVENT.includes(e.type)) {
this.handlePressStart(e)
} else if (MOVE_EVENT.includes(e.type)) {
this.handlePressMove(e)
} else if (MOVE_END_EVENT.includes(e.type)) {
this.handlePressEnd(e)
} else if (MOVE_CANCEL_EVENT.includes(e.type)) {
this.handlePressEnd(e)
}
}
handlePressStart(e) {
standardizationEvent(e)
const timeStamp = Date.now()
this.isPressDown = true
this.scrollerY.pressStart(e.clientY, timeStamp)
this.scrollerX.pressStart(e.clientX, timeStamp)
}
handlePressMove(e) {
standardizationEvent(e)
if (this.isPressDown) {
const timeStamp = Date.now()
this.scrollerY.pressMove(e.clientY, timeStamp)
this.scrollerX.pressMove(e.clientX, timeStamp)
}
}
handlePressEnd(e) {
standardizationEvent(e)
const timeStamp = Date.now()
this.isPressDown = false
this.scrollerY.pressEnd(e.clientY, timeStamp)
this.scrollerX.pressEnd(e.clientX, timeStamp)
}
}
export default ScrollerDomHandler