-
Notifications
You must be signed in to change notification settings - Fork 1
/
material-touch.js
62 lines (57 loc) · 1.78 KB
/
material-touch.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
56
57
58
59
60
61
62
/* Script for triggering element active states when touched
* Supplement to the MaterialZ CSS library
*
* Copyright 2015-2021 Zachary Yaro
* Released under the MIT license
* https://materialz.dev/LICENSE.txt
*/
(function () {
var ELEM_TYPES = ["button", "select"],
INPUT_TYPES = ["button", "checkbox", "radio", "range", "reset", "submit"];
/**
* Check whether a given element is a supported form element.
* @param {HTMLElement} elem
*/
function isSupported(elem) {
// Check whether the element is a supported form element,
return (ELEM_TYPES.indexOf(elem.tagName.toLowerCase()) !== -1 ||
// a supported <input> type,
(elem.type && INPUT_TYPES.indexOf(elem.type.toLowerCase()) !== -1) ||
// or has role="button".
elem.getAttribute('role') === 'button');
}
/**
* Add the “active” class to the touched element
* @param {TouchEvent} e
*/
function makeActive(e) {
if (isSupported(e.target)) {
if(!!e.target.classList) { // use classList API if available
e.target.classList.add("active");
} else {
e.target.className += " active";
}
e.stopPropagation();
}
}
/**
* Remove the “active” class from the touched element
* @param {TouchEvent} e
*/
function makeInactive(e) {
if (isSupported(e.target)) {
if(!!e.target.classList) { // use classList API if available
e.target.classList.remove("active");
} else {
e.target.className = e.srcElement.className.replace(/\s*active/g, "");
}
e.stopPropagation();
}
}
window.addEventListener('load', function(e) {
// Add the event listeners to the body.
document.body.addEventListener('touchstart', makeActive, false);
document.body.addEventListener("touchend", makeInactive, false);
document.body.addEventListener("touchcancel", makeInactive, false);
}, false);
})();