-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (46 loc) · 1.7 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
const body = document.body
const getBodyTheme = localStorage.getItem('portfolio-theme')
const getBtnTheme = localStorage.getItem('portfolio-btn-theme')
const btnTheme = document.querySelector('.fa-moon')
const btnHamburger = document.querySelector('.fa-bars')
const isDark = () => body.classList.contains('dark')
const setTheme = (bodyClass, buttonClass) => {
body.classList.remove(localStorage.getItem('portfolio-theme'))
btnTheme.classList.remove(localStorage.getItem('portfolio-btn-theme'))
body.classList.add(bodyClass)
btnTheme.classList.add(buttonClass)
localStorage.setItem('portfolio-theme', bodyClass)
localStorage.setItem('portfolio-btn-theme', buttonClass)
}
const toggleTheme = () =>
isDark() ? setTheme('light', 'fa-moon') : setTheme('dark', 'fa-sun')
const displayList = () => {
const navUl = document.querySelector('.nav__list')
if (btnHamburger.classList.contains('fa-bars')) {
btnHamburger.classList.remove('fa-bars')
btnHamburger.classList.add('fa-times')
navUl.classList.add('display-nav-list')
} else {
btnHamburger.classList.remove('fa-times')
btnHamburger.classList.add('fa-bars')
navUl.classList.remove('display-nav-list')
}
}
const scrollUp = () => {
const btnScrollTop = document.querySelector('.scroll-top');
if (
body.scrollTop > 500 ||
document.documentElement.scrollTop > 500
) {
btnScrollTop.style.display = 'block';
} else {
btnScrollTop.style.display = 'none';
}
};
btnTheme.addEventListener('click', toggleTheme)
btnHamburger.addEventListener('click', displayList)
document.addEventListener('scroll', scrollUp)
const setBodyTheme = () => body.classList.add(getBodyTheme)
const setBtnTheme = () => btnTheme.classList.add(getBtnTheme)
setBodyTheme()
setBtnTheme()