forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
49 lines (42 loc) · 1.83 KB
/
util.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
(function(global){
'use strict';
global.elem = function elem(name, attrs, children){
attrs = attrs || {};
children = children || [];
var e = document.createElement(name);
Object.keys(attrs).forEach(function(key){
e.setAttribute(key, attrs[key]);
});
children.forEach(function(child){
if (typeof child === 'string'){
child = document.createTextNode(child);
}
e.appendChild(child);
});
return e;
};
if (document.body.matches){
global.matches = function matches(elem, selector){ return elem.matches(selector); };
}else if(document.body.mozMatchesSelector){
global.matches = function matches(elem, selector){ return elem.mozMatchesSelector(selector); };
}else if (document.body.webkitMatchesSelector){
global.matches = function matches(elem, selector){ return elem.webkitMatchesSelector(selector); };
}else if (document.body.msMatchesSelector){
global.matches = function matches(elem, selector){ return elem.msMatchesSelector(selector); };
}else if(document.body.oMatchesSelector){
global.matches = function matches(elem, selector){ return elem.oMatchesSelector(selector); };
}
global.closest = function closest(elem, selector){
while(elem){
if (matches(elem, selector)){ return elem };
elem = elem.parentElement;
}
return null;
};
global.requestAnimationFrame = global.requestAnimationFrame || global.mozRequestAnimationFrame || global.msRequestAnimationFrame || global.webkitRequestAnimationFrame || function(fn){
setTimeout(fn, 20);
};
global.trigger = function trigger(name, target){
target.dispatchEvent(new CustomEvent(name, {bubbles: true, cancelable: false}));
};
})(window);