-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggle.js
64 lines (48 loc) · 1.41 KB
/
toggle.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
63
64
function getRealDisplay(elem) {
if (elem.currentStyle) {
return elem.currentStyle.display;
} else if (window.getComputedStyle) {
var computedStyle = window.getComputedStyle(elem, null );
return computedStyle.getPropertyValue('display');
}
}
function hide(el) {
if (!el.getAttribute('displayOld')) {
el.setAttribute("displayOld", el.style.display);
}
el.style.display = "none";
}
displayCache = {};
function isHidden(el) {
var width = el.offsetWidth,
height = el.offsetHeight,
tr = el.nodeName.toLowerCase() === "tr";
return width === 0 && height === 0 && !tr ?
true : width > 0 && height > 0 && !tr ? false : getRealDisplay(el);
}
function toggle(element) {
var el = document.getElementById(element);
isHidden(el) ? show(el) : hide(el);
}
function show(el) {
if (getRealDisplay(el) != 'none') return;
var old = el.getAttribute("displayOld");
el.style.display = old || "";
if ( getRealDisplay(el) === "none" ) {
var nodeName = el.nodeName, body = document.body, display;
if ( displayCache[nodeName] ) {
display = displayCache[nodeName];
} else {
var testElem = document.createElement(nodeName);
body.appendChild(testElem);
display = getRealDisplay(testElem);
if (display === "none" ) {
display = "block";
}
body.removeChild(testElem);
displayCache[nodeName] = display;
}
el.setAttribute('displayOld', display);
el.style.display = display;
}
}