-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (65 loc) · 2.22 KB
/
Copy pathscript.js
File metadata and controls
82 lines (65 loc) · 2.22 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
// Botón Ir arriba
window.onscroll = function() {
mostrarOcultarBoton();
};
function mostrarOcultarBoton() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("volver-arriba").style.display = "block";
} else {
document.getElementById("volver-arriba").style.display = "none";
}
}
document.getElementById("volver-arriba").onclick = function(e) {
e.preventDefault();
window.scrollTo({
top: 0,
behavior: "smooth"
});
};
// Obtén el modal
const modal = document.getElementById("modal-imagen");
// Obtén todas las imágenes con el atributo data-modal-imagen
const imagenes = document.querySelectorAll("[data-modal-imagen]");
// Obtén el elemento <img> dentro del modal
const modalImagen = document.getElementById("img01");
// Obtén el elemento <p> para el texto del atributo alt
const captionText = document.getElementById("caption");
// Obtén el elemento <span> que cierra el modal
const cerrar = document.getElementsByClassName("cerrar")[0];
// Obtén las flechas de navegación
const anterior = document.querySelector(".anterior");
const siguiente = document.querySelector(".siguiente");
let indiceActual = 0;
// Función para actualizar la imagen y el texto del modal
function actualizarModal(indice) {
modalImagen.src = imagenes[indice].dataset.modalImagen;
captionText.innerHTML = imagenes[indice].alt;
}
// Agrega un evento click a cada imagen
imagenes.forEach((imagen, indice) => {
imagen.onclick = function() {
modal.style.display = "flex";
indiceActual = indice;
actualizarModal(indiceActual);
}
});
// Agrega un evento click a la flecha anterior
anterior.onclick = function() {
indiceActual = (indiceActual - 1 + imagenes.length) % imagenes.length;
actualizarModal(indiceActual);
}
// Agrega un evento click a la flecha siguiente
siguiente.onclick = function() {
indiceActual = (indiceActual + 1) % imagenes.length;
actualizarModal(indiceActual);
}
// Agrega un evento click al botón de cerrar
cerrar.onclick = function() {
modal.style.display = "none";
}
// Agrega un evento click al fondo del modal
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}