This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
tabs.html
88 lines (70 loc) · 3.5 KB
/
tabs.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!--
3 - Tabs (4/3/2020)
Tabbed panels are widely used in user interfaces. They allow you to select an interface panel by choosing from a number of tabs “sticking out” above an element.
In this exercise you must implement a simple tabbed interface. Write a function, asTabs, that takes a DOM node and creates a tabbed interface showing the child elements of that node. It should insert a list of <button> elements at the top of the node, one for each child element, containing text retrieved from the data-tabname attribute of the child. All but one of the original children should be hidden (given a display style of none). The currently visible node can be selected by clicking the buttons.
When that works, extend it to style the button for the currently selected tab differently so that it is obvious which tab is selected.
-->
<style type="text/css">
tab-panel > button.selected{
background: #FF00FF;
color: #FFFFFF;
}
tab-panel > div[data-tabname].hidden {
display: none;
}
</style>
<body>
<tab-panel>
<div data-tabname="one">Tab one</div>
<div data-tabname="two">Tab two</div>
<div data-tabname="three">Tab three</div>
</tab-panel>
<script>
// Keeps track of the currently selected tab.
let activeTab;
// Keeps track of the current body of tab to be displayed.
let currentBody;
function switchTab(event) {
// Switches the `activeTab` binding to the newly selected tab, and
// removes the `selected` class from the previously selected tab while
// adding it to the newly switched `activeTab`.
activeTab.classList.remove("selected");
activeTab = event.target;
activeTab.classList.add("selected");
let tabContent = document.querySelectorAll("div");
// Hides the content of the previously selected tab.
currentBody.classList.toggle("hidden");
// Manages display of the contents of the multiple tabbed sections.
switch(activeTab.textContent) {
case "one":
currentBody = tabContent[0];
break;
case "two":
currentBody = tabContent[1];
break;
case "three":
currentBody = tabContent[2];
break;
}
// Displays the content of the currently selected tab.
currentBody.classList.toggle("hidden");
}
function asTabs(node) {
let tabLength = node.children.length;
for (let i = 0; i < tabLength; i++) {
let tabButton = document.createElement("button");
tabButton.textContent = document.querySelectorAll("div")[i].getAttribute("data-tabname");
// By default, hide the body of the tabs.
node.querySelectorAll("div")[i].classList.add("hidden");
tabButton.addEventListener("click", switchTab);
node.insertBefore(tabButton, document.querySelector("div"));
}
// Sets the initially selected tab & display its associated content.
activeTab = document.querySelector("button");
activeTab.classList.add("selected");
currentBody = document.querySelector("div");
currentBody.classList.toggle("hidden");
}
asTabs(document.querySelector("tab-panel"));
</script>
</body>