-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclasslist.js
55 lines (44 loc) · 1.62 KB
/
classlist.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
/*
* Minimal classList shim for IE 9
* By Devon Govett
* MIT LICENSE
* @see https://gist.github.com/devongovett/1381839
*/
if (!('classList' in document.documentElement) && Object.defineProperty && (typeof HTMLElement !== 'undefined')) {
Object.defineProperty(HTMLElement.prototype, 'classList', {
get: function () {
var self = this;
function update(fn) {
return function (value) {
var classes = self.className.split(/\s+/),
index = classes.indexOf(value);
fn(classes, index, value);
self.className = classes.join(" ");
}
}
var ret = {
add: update(function (classes, index, value) {
~index || classes.push(value);
}),
remove: update(function (classes, index) {
~index && classes.splice(index, 1);
}),
toggle: update(function (classes, index, value) {
~index ? classes.splice(index, 1) : classes.push(value);
}),
contains: function (value) {
return !!~self.className.split(/\s+/).indexOf(value);
},
item: function (i) {
return self.className.split(/\s+/)[i] || null;
}
};
Object.defineProperty(ret, 'length', {
get: function () {
return self.className.split(/\s+/).length;
}
});
return ret;
}
});
}