-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
152 lines (124 loc) · 5.34 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
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
148
149
150
151
152
/*==================== SHOW MENU ====================*/
const showMenu = (toggleId, navId) =>{
const toggle = document.getElementById(toggleId),
nav = document.getElementById(navId)
// Validate that variables exist
if(toggle && nav){
toggle.addEventListener('click', ()=>{
// We add the show-menu class to the div tag with the nav__menu class
nav.classList.toggle('show-menu')
})
}
}
showMenu('nav-toggle','nav-menu')
/*==================== REMOVE MENU MOBILE ====================*/
const navLink = document.querySelectorAll('.nav__link')
function linkAction(){
const navMenu = document.getElementById('nav-menu')
// When we click on each nav__link, we remove the show-menu class
if (navMenu) {
navMenu.classList.remove('show-menu')
}
}
if (navLink) {
navLink.forEach(n => n.addEventListener('click', linkAction))
}
/*==================== SCROLL SECTIONS ACTIVE LINK ====================*/
const sections = document.querySelectorAll('section[id]')
function scrollActive(){
const scrollY = window.pageYOffset
sections.forEach(current =>{
const sectionHeight = current.offsetHeight
const sectionTop = current.offsetTop - 50;
const sectionId = current.getAttribute('id')
const nav__ = document.querySelector('.nav__menu a[href*=' + sectionId + ']')
if (nav__) {
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
nav__.classList.add('active-link')
}else{
document.querySelector('.nav__menu a[href*=' + sectionId + ']').classList.remove('active-link')
}
}
})
}
window.addEventListener('scroll', scrollActive)
/*==================== CHANGE BACKGROUND HEADER ====================*/
function scrollHeader(){
const nav = document.getElementById('header')
// When the scroll is greater than 200 viewport height, add the scroll-header class to the header tag
if(this.scrollY >= 200) nav.classList.add('scroll-header'); else nav.classList.remove('scroll-header')
}
window.addEventListener('scroll', scrollHeader)
/*==================== SHOW SCROLL TOP ====================*/
function scrollTop(){
const scrollTop = document.getElementById('scroll-top');
// When the scroll is higher than 560 viewport height, add the show-scroll class to the a tag with the scroll-top class
if(!scrollTop) return;
if(this.scrollY >= 560) scrollTop.classList.add('show-scroll'); else scrollTop.classList.remove('show-scroll')
}
window.addEventListener('scroll', scrollTop)
/*==================== DARK LIGHT THEME ====================*/
const themeButton = document.getElementById('theme-button')
const darkTheme = 'dark-theme'
const iconTheme = 'bx-toggle-right'
// Previously selected topic (if user selected)
const selectedTheme = localStorage.getItem('selected-theme')
const selectedIcon = localStorage.getItem('selected-icon')
// We obtain the current theme that the interface has by validating the dark-theme class
const getCurrentTheme = () => document.body.classList.contains(darkTheme) ? 'dark' : 'light'
const getCurrentIcon = () => themeButton.classList.contains(iconTheme) ? 'bx-toggle-left' : 'bx-toggle-right'
// We validate if the user previously chose a topic
if (selectedTheme) {
// If the validation is fulfilled, we ask what the issue was to know if we activated or deactivated the dark
document.body.classList[selectedTheme === 'dark' ? 'add' : 'remove'](darkTheme)
themeButton.classList[selectedIcon === 'bx-toggle-left' ? 'add' : 'remove'](iconTheme)
}
// Activate / deactivate the theme manually with the button
themeButton.addEventListener('click', () => {
// Add or remove the dark / icon theme
document.body.classList.toggle(darkTheme)
themeButton.classList.toggle(iconTheme)
// We save the theme and the current icon that the user chose
localStorage.setItem('selected-theme', getCurrentTheme())
localStorage.setItem('selected-icon', getCurrentIcon())
})
/*==================== SCROLL REVEAL ANIMATION ====================*/
// check if function named ScrollReveal exists
if (typeof ScrollReveal !== 'undefined') {
if (ScrollReveal()) {
const sr = ScrollReveal({
distance: '30px',
duration: 1800,
});
sr.reveal(`.home__data, .home__img, .team__head, .team-card,
.decoration__data,
.accessory__content,
.footer__content`, {
origin: 'top',
interval: 200,
})
sr.reveal(`.share__img, .send__content`, {
origin: 'left'
})
sr.reveal(`.share__data, .send__img`, {
origin: 'right'
})
}
}
let getStarted = document.querySelector(".home__data .button");
if(getStarted) getStarted.addEventListener("click", getStartedHandler);
function getStartedHandler(){
window.location.href = "/signIn"
}
let learnMore = document.querySelector(".share__container a");
if(learnMore) learnMore.addEventListener("click", learnMoreHandler);
function learnMoreHandler(){
window.location.href = "/learnMore"
}
let goShopping = document.querySelectorAll(".decoration__data a");
for(let i=0;i<goShopping.length;i++){
goShopping[i].addEventListener("click", goShoppingHandler);
}
function goShoppingHandler(){
window.location.href = "/productsPage"
}