-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (98 loc) · 4.12 KB
/
script.js
File metadata and controls
143 lines (98 loc) · 4.12 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
/*=============== SHOW MENU ===============*/
const navMenu= document.getElementById('nav-menu'),
navToggle= document.getElementById('nav-toggle'),
navClose= document.getElementById('nav-close')
/*=============== MENU SHOW ===============*/
if(navToggle){
navToggle.addEventListener('click', () =>{
navMenu.classList.add('show-menu')
})
}
/*=============== MENU HIDDEN ===============*/
if(navClose){
navClose.addEventListener('click', () =>{
navMenu.classList.remove('show-menu')
})
}
/*=============== REMOVE MENU MOBILE ===============*/
const navLink = document.querySelectorAll('.nav__link')
const linkAction = () =>{
const navMenu = document.getElementById('nav-menu')
navMenu.classList.remove('show-menu')
}
navLink.forEach(n => n.addEventListener('click', linkAction))
/*=============== CHANGE BACKGROUND HEADER ===============*/
const scrollHeader = () =>{
const header = document.getElementById('header')
this.scrollY >= 50 ? header.classList.add('scroll-header')
:header.classList.remove('scroll-header')
}
window.addEventListener('scroll', scrollHeader)
/*=============== SHADOW HEADER ===============*/
const shadowHeader = () =>{
const header = document.getElementById('header')
this.scrollY >= 50 ? header.classList.add('shadow-header')
:header.classList.remove('shadow-header')
}
window.addEventListener('scroll', shadowHeader)
/*=============== EMAIL JS ===============*/
const contactForm = document.getElementById('contact-form'),
contactMessage = document.getElementById('contact-message')
const sendEmail =(e) =>{
e.preventDefault()
//Show sent message
emailjs.sendForm('service_9cdov8h','template_byqak89','#contact-form','Zd5d26ituPIDAzf4H')
.then(() =>{
contactMessage.textContent = 'Message sent successfully ✅'
//Message remove timeout
setTimeout(() =>{
contactMessage.textContent = ''
}, 5000)
//Clear Fields
contactForm.reset()
}, () =>{
//Show error message
contactMessage.textContent = 'Message not sent (service error) ❌'
})
}
contactForm.addEventListener('submit', sendEmail)
const scrollUp = () =>{
const scrollUp = document.getElementById('scroll-up')
this.scrollY >= 350 ? scrollUp.classList.add('show-scroll')
: scrollUp.classList.remove('show-scroll')
}
window.addEventListener('scroll', scrollUp)
const sections = document.querySelectorAll('section[id]')
const scrollActive = () =>{
const scrollDown = window.scrollY
sections.forEach(current =>{
const sectionHeight = current.offsetHeight,
sectionTop =current.offsetTop - 58,
sectionId = current.getAttribute('id'),
sectionClass = document.querySelector(' .nav__menu a[href*=' + sectionId + ']')
if(scrollDown > sectionTop && scrollDown <= sectionTop + sectionHeight){
sectionClass.classList.add('active-link')
}else{
sectionClass.classList.remove('active-link')
}
})
}
window.addEventListener('scroll', scrollActive)
/*=============== DARK MODE ===============*/
const themeButton = document.getElementById('theme-button')
const darkTheme = 'dark-theme'
const iconTheme = 'ri-sun-line'
const selectedTheme = localStorage.getItem('selected-theme')
const selectedIcon = localStorage.getItem('selected-icon')
const getCurrentTheme = () => document.body.classList.contains(darkTheme) ? 'dark' : 'light'
const getCurrentIcon = () => themeButton.classList.contains(iconTheme) ? 'ri-moon-line' : 'ri-sun-line'
if(selectedTheme) {
document.body.classList[selectedTheme === 'dark' ? 'add' : 'remove'](darkTheme)
themeButton.classList[selectedIcon === 'ri-moon-line' ? 'add' : 'remove'](iconTheme)
}
themeButton.addEventListener('click',() => {
document.body.classList.toggle(darkTheme)
themeButton.classList.toggle(iconTheme)
localStorage.setItem('selected-theme', getCurrentTheme())
localStorage.setItem('selected-icon', getCurrentIcon())
})