-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
53 lines (44 loc) · 1.86 KB
/
Copy pathscripts.js
File metadata and controls
53 lines (44 loc) · 1.86 KB
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
var accordionItems = new Array();
// Grab the accordion items from the page
accordionItems = document.querySelectorAll('.accordion-item');
// Assign onclick events to the accordion item headings
for (var i = 0; i < accordionItems.length; i++) {
var h2 = getFirstChildWithTagName(accordionItems[i], 'H2');
h2.onclick = toggleItem;
}
// Hide all accordion item bodies except the first
for (var i = 1; i < accordionItems.length; i++) {
accordionItems[i].className = 'accordion-item hide';
}
// Add exact height to div that is shown on page load
var firstDiv = accordionItems[0].getElementsByTagName('div')[0];
firstDiv.style.height = "auto";
var firstDivHeight = firstDiv.offsetHeight;
firstDiv.style.height = firstDivHeight + 'px';
function toggleItem() {
var itemClass = this.parentNode.className;
// Hide all items
for (var i = 0; i < accordionItems.length; i++) {
accordionItems[i].className = 'accordion-item hide';
accordionItems[i].getElementsByTagName('div')[0].style.height = "0";
}
// Show this item if it was previously hidden
if (itemClass == 'accordion-item hide') {
var accordionItemContent = this;
do accordionItemContent = accordionItemContent.nextSibling; while(accordionItemContent && accordionItemContent.nodeType !== 1);
accordionItemContent.style.height = "auto";
var divHeight = accordionItemContent.offsetHeight;
accordionItemContent.style.height = "";
this.parentNode.className = 'accordion-item';
var that = this;
window.setTimeout(function () {
accordionItemContent.style.height = divHeight + 'px';
}, 50);
}
}
// Helper function
function getFirstChildWithTagName(element, tagName) {
for (var i = 0; i < element.childNodes.length; i++) {
if (element.childNodes[i].nodeName == tagName) return element.childNodes[i];
}
}