-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (42 loc) · 1.49 KB
/
app.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
const links = document.querySelectorAll('[data-to]');
const divContents = document.getElementsByClassName('content');
const pageLinks = document.querySelectorAll('[data-page]');
const pageContents = document.getElementsByClassName('list-page');
for (const link of links) {
link.addEventListener('click', ({ target }) => {
const id = target.dataset.content;
for (const divContent of divContents) {
if (id === divContent.id) {
divContent.classList.add('active');
} else {
divContent.classList.remove('active');
}
}
for (const link of links) {
link.classList.remove('active');
}
target.classList.add('active');
// Modificar la URL
const url = target.dataset.to;
history.pushState({}, '', url);
});
}
// Evento click de las páginas
for (const pageLink of pageLinks) {
pageLink.addEventListener('click', () => {
const page = pageLink.dataset.page;
for (const pageContent of pageContents) {
if (pageContent.id === `page-${page}`) {
pageContent.classList.add('active');
} else {
pageContent.classList.remove('active');
}
}
for (const link of pageLinks) {
link.parentElement.classList.remove('active');
}
pageLink.parentElement.classList.add('active');
const url = `/list/${page}`;
history.pushState({}, '', url);
});
}