-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab-experiment.html
More file actions
147 lines (132 loc) · 6.96 KB
/
tab-experiment.html
File metadata and controls
147 lines (132 loc) · 6.96 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
</head>
</head>
<body>
<div class="tab-content">
<div class="tab-pane active content-1" role="tabpanel" aria-labelledby="content-1-tab" tabindex="0">
My Tab 1 Content 2
</div>
<div class="tab-pane content-2" role="tabpanel" aria-labelledby="content-2-tab" tabindex="0">
My Tab 2 Content 2
</div>
<div class="tab-pane content-3" role="tabpanel" aria-labelledby="content-3-tab" tabindex="0">
My Tab 3 Content 2
</div>
<div class="tab-pane content-4" role="tabpanel" aria-labelledby="content-4-tab" tabindex="0">
My Tab 4 Content 2
</div>
<div class="tab-pane content-5" role="tabpanel" aria-labelledby="content-5-tab" tabindex="0">
My Tab 5 Content 2
</div>
</div>
<ul class="nav nav-tabs" id="multiple-tab-contents" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="content-1-tab" data-bs-toggle="tab" data-bs-target=".content-1" type="button" role="tab" aria-selected="true" data-current-target=".content-1">
Tab 1
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="content-2-tab" data-bs-toggle="tab" data-bs-target=".content-2" type="button" role="tab" aria-selected="false" data-current-target=".content-1">
Tab 2
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="content-3-tab" data-bs-toggle="tab" data-bs-target=".content-3" type="button" role="tab" aria-selected="false" data-current-target=".content-1">
Tab 3
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="content-4-tab" data-bs-toggle="tab" data-bs-target=".content-4" type="button" role="tab" aria-selected="false" data-current-target=".content-1">
Tab 4
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="content-5-tab" data-bs-toggle="tab" data-bs-target=".content-5" type="button" role="tab" aria-selected="false" data-current-target=".content-1">
Tab 5
</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active content-1" role="tabpanel" aria-labelledby="content-1-tab" tabindex="0">
My Tab 1 Content 1
</div>
<div class="tab-pane content-2" role="tabpanel" aria-labelledby="content-2-tab" tabindex="0">
My Tab 2 Content 1
</div>
<div class="tab-pane content-3" role="tabpanel" aria-labelledby="content-3-tab" tabindex="0">
My Tab 3 Content 1
</div>
<div class="tab-pane content-4" role="tabpanel" aria-labelledby="content-4-tab" tabindex="0">
My Tab 4 Content 1
</div>
<div class="tab-pane content-5" role="tabpanel" aria-labelledby="content-5-tab" tabindex="0">
My Tab 5 Content 1
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<script type="text/javascript">
const activeClass = 'active';
const tabButtonsQuerySelector = '#multiple-tab-contents button';
// for each button, we want to add a 'click' event listener to override boostrap behavior
document.querySelectorAll(tabButtonsQuerySelector).forEach((tabButton) => {
tabButton.addEventListener('click', (evt) => {
evt.preventDefault();
evt.stopPropagation();
// targetQuery is the value inserted on <button ... data-bs-target="">
const targetQuery = evt.target.getAttribute('data-bs-target');
// set the targetQuery on the attribute <button ... data-current-target=""> of all buttons
document.querySelectorAll(tabButtonsQuerySelector).forEach((btn) => {
btn.setAttribute('data-current-target', targetQuery);
});
// for each data-bs-target, we want to add activeClass on the target and remove from its siblings
document.querySelectorAll(targetQuery).forEach((target) => {
// add activeClass
enableTabContent(target);
// remove activeClass from previous siblings
disablePreviousSiblings(target.previousElementSibling);
// remove activeClass from next siblings
disableNextSiblings(target.nextElementSibling);
})
});
});
function enableTabContent(target) {
// if it already have the class, ignore it
if (target.className.includes(' ' + activeClass)) {
return;
}
// if it doesn't, append the activeClass in the end
target.className = target.className + ' ' + activeClass;
}
function disablePreviousSiblings(sibling) {
// if there's no previous sibling, stop
if (!sibling) {
return;
}
// if the previous sibling has the active class
if (sibling.className.includes(' ' + activeClass)) {
// remove it
sibling.className = sibling.className.replace(' ' + activeClass, '');
}
// check the "next" previous siblings (recursive function)
disablePreviousSiblings(sibling.previousElementSibling);
}
function disableNextSiblings(sibling) {
// if there's no next sibling, stop
if (!sibling) {
return;
}
// if the next sibling has the active class
if (sibling.className.includes(' ' + activeClass)) {
// remove it
sibling.className = sibling.className.replace(' ' + activeClass, '');
}
// check the "next" next siblings (recursive function)
disableNextSiblings(sibling.nextElementSibling);
}
</script>
</body>
</html>