forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.js
More file actions
114 lines (94 loc) · 2.32 KB
/
dom.js
File metadata and controls
114 lines (94 loc) · 2.32 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
export function offset (el) {
if (el === window) {
return {top: 0, left: 0}
}
let {top, left} = el.getBoundingClientRect()
return {top, left}
}
export function style (el, property) {
return window.getComputedStyle(el).getPropertyValue(property)
}
export function height (el) {
if (el === window) {
return viewport().height
}
return parseFloat(window.getComputedStyle(el).getPropertyValue('height'), 10)
}
export function width (el) {
if (el === window) {
return viewport().width
}
return parseFloat(window.getComputedStyle(el).getPropertyValue('width'), 10)
}
export function css (element, css) {
let style = element.style
Object.keys(css).forEach(prop => {
style[prop] = css[prop]
})
}
export function viewport () {
let
e = window,
a = 'inner'
if (!('innerWidth' in window)) {
a = 'client'
e = document.documentElement || document.body
}
return {
width: e[a + 'Width'],
height: e[a + 'Height']
}
}
export function ready (fn) {
if (typeof fn !== 'function') {
return
}
if (document.readyState === 'complete') {
return fn()
}
document.addEventListener('DOMContentLoaded', fn, false)
}
export function getScrollTarget (el) {
return el.closest('.layout-view,.scroll') || window
}
export function getScrollPosition (scrollTarget) {
if (scrollTarget === window) {
return window.pageYOffset || window.scrollY || document.body.scrollTop || 0
}
return scrollTarget.scrollTop
}
function animScrollTo (el, to, duration) {
if (duration <= 0) {
return
}
const pos = getScrollPosition(el)
requestAnimationFrame(() => {
setScroll(el, pos + (to - pos) / duration * 16)
if (el.scrollTop !== to) {
animScrollTo(el, to, duration - 16)
}
})
}
function setScroll (scrollTarget, offset) {
if (scrollTarget === window) {
document.documentElement.scrollTop = offset
document.body.scrollTop = offset
return
}
scrollTarget.scrollTop = offset
}
export function setScrollPosition (scrollTarget, offset, duration) {
if (duration) {
animScrollTo(scrollTarget, offset, duration)
return
}
setScroll(scrollTarget, offset)
}
const prefix = ['-webkit-', '-moz-', '-ms-', '-o-']
export function cssTransform (val) {
let o = {transform: val}
prefix.forEach(p => {
o[p + 'transform'] = val
})
return o
}