dom.js
4.52 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
import { inBrowser, isWeChatDevTools } from './env'
import { extend } from './lang'
let elementStyle = inBrowser && document.createElement('div').style
let vendor = (() => {
if (!inBrowser) {
return false
}
let transformNames = {
webkit: 'webkitTransform',
Moz: 'MozTransform',
O: 'OTransform',
ms: 'msTransform',
standard: 'transform'
}
for (let key in transformNames) {
if (elementStyle[transformNames[key]] !== undefined) {
return key
}
}
return false
})()
function prefixStyle(style) {
if (vendor === false) {
return false
}
if (vendor === 'standard') {
if (style === 'transitionEnd') {
return 'transitionend'
}
return style
}
return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}
export function addEvent(el, type, fn, capture) {
el.addEventListener(type, fn, {passive: false, capture: !!capture})
}
export function removeEvent(el, type, fn, capture) {
el.removeEventListener(type, fn, {passive: false, capture: !!capture})
}
export function offset(el) {
let left = 0
let top = 0
while (el) {
left -= el.offsetLeft
top -= el.offsetTop
el = el.offsetParent
}
return {
left,
top
}
}
export function offsetToBody(el) {
let rect = el.getBoundingClientRect()
return {
left: -(rect.left + window.pageXOffset),
top: -(rect.top + window.pageYOffset)
}
}
export const cssVendor = (vendor && vendor !== 'standard') ? '-' + vendor.toLowerCase() + '-' : ''
let transform = prefixStyle('transform')
let transition = prefixStyle('transition')
export const hasPerspective = inBrowser && prefixStyle('perspective') in elementStyle
// fix issue #361
export const hasTouch = inBrowser && ('ontouchstart' in window || isWeChatDevTools)
export const hasTransform = transform !== false
export const hasTransition = inBrowser && transition in elementStyle
export const style = {
transform,
transition,
transitionTimingFunction: prefixStyle('transitionTimingFunction'),
transitionDuration: prefixStyle('transitionDuration'),
transitionDelay: prefixStyle('transitionDelay'),
transformOrigin: prefixStyle('transformOrigin'),
transitionEnd: prefixStyle('transitionEnd')
}
export const TOUCH_EVENT = 1
export const MOUSE_EVENT = 2
export const eventType = {
touchstart: TOUCH_EVENT,
touchmove: TOUCH_EVENT,
touchend: TOUCH_EVENT,
mousedown: MOUSE_EVENT,
mousemove: MOUSE_EVENT,
mouseup: MOUSE_EVENT
}
export function getRect(el) {
if (el instanceof window.SVGElement) {
let rect = el.getBoundingClientRect()
return {
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height
}
} else {
return {
top: el.offsetTop,
left: el.offsetLeft,
width: el.offsetWidth,
height: el.offsetHeight
}
}
}
export function preventDefaultException(el, exceptions) {
for (let i in exceptions) {
if (exceptions[i].test(el[i])) {
return true
}
}
return false
}
export function tap(e, eventName) {
let ev = document.createEvent('Event')
ev.initEvent(eventName, true, true)
ev.pageX = e.pageX
ev.pageY = e.pageY
e.target.dispatchEvent(ev)
}
export function click(e, event = 'click') {
let eventSource
if (e.type === 'mouseup' || e.type === 'mousecancel') {
eventSource = e
} else if (e.type === 'touchend' || e.type === 'touchcancel') {
eventSource = e.changedTouches[0]
}
let posSrc = {}
if (eventSource) {
posSrc.screenX = eventSource.screenX || 0
posSrc.screenY = eventSource.screenY || 0
posSrc.clientX = eventSource.clientX || 0
posSrc.clientY = eventSource.clientY || 0
}
let ev
const bubbles = true
const cancelable = true
if (typeof MouseEvent !== 'undefined') {
try {
ev = new MouseEvent(event, extend({
bubbles,
cancelable
}, posSrc))
} catch (e) {
createEvent()
}
} else {
createEvent()
}
function createEvent() {
ev = document.createEvent('Event')
ev.initEvent(event, bubbles, cancelable)
extend(ev, posSrc)
}
// forwardedTouchEvent set to true in case of the conflict with fastclick
ev.forwardedTouchEvent = true
ev._constructed = true
e.target.dispatchEvent(ev)
}
export function dblclick(e) {
click(e, 'dblclick')
}
export function prepend(el, target) {
if (target.firstChild) {
before(el, target.firstChild)
} else {
target.appendChild(el)
}
}
export function before(el, target) {
target.parentNode.insertBefore(el, target)
}
export function removeChild(el, child) {
el.removeChild(child)
}