-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
86 lines (75 loc) · 2.98 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
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
const btn = document.getElementById('button');
const sectionAll = document.querySelectorAll('section[id]');
const inputName = document.querySelector('#nombre');
const inputEmail = document.querySelector('#email');
const flagsElement = document.getElementById('flags');
const textsToChange = document.querySelectorAll('[data-section]');
/* ===== Loader =====*/
window.addEventListener('load', () => {
const contenedorLoader = document.querySelector('.container--loader');
contenedorLoader.style.opacity = 0;
contenedorLoader.style.visibility = 'hidden';
})
/*===== Header =====*/
window.addEventListener('scroll', () => {
const header = document.querySelector('header');
header.classList.toggle('abajo', window.scrollY > 0);
});
/*===== Boton Menu =====*/
btn.addEventListener('click', function() {
if (this.classList.contains('active')) {
this.classList.remove('active');
this.classList.add('not-active');
document.querySelector('.nav_menu').classList.remove('active');
document.querySelector('.nav_menu').classList.add('not-active');
}
else {
this.classList.add('active');
this.classList.remove('not-active');
document.querySelector('.nav_menu').classList.remove('not-active');
document.querySelector('.nav_menu').classList.add('active');
}
});
/*===== Cambio de idioma =====*/
const changeLanguage = async language => {
const requestJson = await fetch(`./languages/${language}.json`);
const texts = await requestJson.json();
for(const textToChange of textsToChange) {
const section = textToChange.dataset.section;
const value = textToChange.dataset.value;
textToChange.innerHTML = texts[section][value];
}
}
flagsElement.addEventListener('click', (e) => {
changeLanguage(e.target.parentElement.dataset.language);
})
/*===== class active por secciones =====*/
window.addEventListener('scroll', () => {
const scrollY = window.pageYOffset;
sectionAll.forEach((current) => {
const sectionHeight = current.offsetHeight;
const sectionTop = current.offsetTop - 100;
const sectionId = current.getAttribute('id');
if (scrollY > sectionTop && scrollY < sectionTop + sectionHeight) {
document.querySelector('nav a[href*=' + sectionId + ']').classList.add('active');
}
else {
document.querySelector('nav a[href*=' + sectionId + ']').classList.remove('active');
}
});
});
/*===== Boton y función ir arriba =====*/
window.onscroll = function() {
if (document.documentElement.scrollTop > 100) {
document.querySelector('.go-top-container').classList.add('show');
}
else {
document.querySelector('.go-top-container').classList.remove('show');
}
}
document.querySelector('.go-top-container').addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});