-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (45 loc) · 1.08 KB
/
script.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
const $ = document.querySelector.bind(document)
function TabNavigation() {
const html = {
links: [...$('.tab-links').children],
contents: [...$('.tab-content').children],
openTab: $('[data-open]')
}
function hideAllTabContent() {
html.contents.forEach(section => {
section.style.display = 'none'
})
}
function removeAllActiveClass() {
html.links.forEach(tab => {
tab.className = tab.className.replace("active", "")
})
}
function showCurrentTab(id) {
const tabcontent = $('#' + id)
tabcontent.style.display = 'block'
}
function selectTab(event) {
hideAllTabContent()
const target = event.currentTarget
showCurrentTab(target.dataset.id)
target.className += "active"
}
function listenForChange() {
html.links.forEach(tab => {
tab.addEventListener('click', selectTab)
})
}
function init() {
hideAllTabContent()
listenForChange()
html.openTab.click()
}
return {
init
}
}
window.addEventListener('load', () => {
const tabNavigation = TabNavigation()
tabNavigation.init()
})