-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (41 loc) · 1.31 KB
/
index.ts
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
/**
* vendor prefixes that being taken into consideration.
*/
export const vendors = ['webkit', 'ms', 'moz', 'o'];
/**
* get vendor property name that contains uppercase letter.
* e.g. webkitTransform
* @param prop property name. for example: transform
* @param host optional. property owner. default to `document.body.style`.
*/
export function getProperty(prop: string, host?: HTMLElement | Window | Navigator) {
const targetHost = host || document.body.style;
if (!(prop in targetHost)) {
const char1 = prop.charAt(0).toUpperCase();
const charLeft = prop.substr(1);
for (var i = 0; i < vendors.length; i++) {
const vendorProp = vendors[i] + char1 + charLeft;
if (vendorProp in targetHost) {
return vendorProp;
}
}
}
return prop;
}
/**
* get vendor event property name omitting `on` prefix.
* @param prop property name. for example: animationend.
* @param host optional. property owner. default to `document.body`.
*/
export function getEventProperty(prop: string, host?: HTMLElement | Window) {
const targetHost = host || document.body;
if (!(('on' + prop) in targetHost)) {
for (var i = 0; i < vendors.length; i++) {
const vendorProp = vendors[i] + prop;
if (('on' + vendorProp) in targetHost) {
return vendorProp;
}
}
}
return prop;
}