-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
46 lines (39 loc) · 1.33 KB
/
main.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
void function() {
"use strict"
const $ = sel => document.querySelector(sel)
const $$ = sel => Array.from(document.querySelectorAll(sel))
Node.prototype.on = Node.prototype.addEventListener
let tabs = $$('.tabs .items .item')
let imgs = $$('.tabs .images img')
function updateTab(activeTab) {
tabs.forEach((tab, i) => {
const visible = tab.classList.toggle('active', tab === activeTab)
imgs[i].style.display = visible ? '' : 'none'
});
}
tabs.forEach(tab => tab.on('click', updateTab.bind(null, tab)))
const langKey = "flat:language"
const languages = [
{ key: "zh-CN", name: "简体中文" },
{ key: "en", name: "English" },
]
const langHref = {
"zh-CN": "/",
"en": "/en/",
}
const currentLanguage = location.pathname.split("/").filter(Boolean)[0] || "zh-CN"
let select = $('#lang')
select.innerHTML = ""
languages.forEach(({ key, name }) => {
const option = document.createElement('option')
option.value = key
option.textContent = name
select.append(option)
})
select.value = currentLanguage
select.on('change', (e) => {
const language = e.target.value
localStorage.setItem(langKey, language)
window.location.href = langHref[language]
})
}();