-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (68 loc) · 2.13 KB
/
Copy pathscript.js
File metadata and controls
77 lines (68 loc) · 2.13 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
/* ----- Vanilla JS ----- */
/* Obtención de elementos */
const h = document.getElementById("hamburguesa");
const m = document.getElementById("menu");
const s = document.getElementById("subir");
/* Asignación de eventos */
h.onclick = hamburguesa;
s.onclick = subir;
window.onresize = responsive;
window.onscroll = boton;
/* Funciones */
// Muestra u oculta el Menú de la página
function hamburguesa() {
// Animación del botón de hamburguesa
h.classList.toggle("cambio");
// Visualización del Menú
if (m.style.display === "block") {
m.style.display = "none"; // Oculto
} else {
m.style.display = "block"; // Visible
}
// Cambia el atributo aria-expanded según el estado del Menú
h.setAttribute(
"aria-expanded",
h.getAttribute("aria-expanded") === true ? false : true
);
}
// Sube al inicio de la página con scroll progresivo suave
function subir() {
// Posición del scroll vertical respecto al inicio de la página
let t = document.documentElement.scrollTop;
if (t > 0) {
// Si no está en el inicio...
// Animación de scroll
window.requestAnimationFrame(subir); // Recursivo
// Ir arriba a velocidad decreciente
window.scrollTo(0, t - t / 10);
}
}
// Visualiza u oculta el Menú según resolución
function responsive() {
// Anchura de la ventana
let ancho = window.innerWidth;
// Visualización del Menú
if (ancho > 767) {
m.style.display = "block"; // Visible
h.setAttribute("aria-expanded", true); // Accesibilidad
} else {
// Si '.cambio' está presente, se quita para cerrar las aspas
if (h.classList.contains("cambio")) {
h.classList.remove("cambio");
}
m.style.display = "none"; // Oculto
h.setAttribute("aria-expanded", false); // Accesibilidad
}
}
// Muestra u oculta el botón Subir según posición en el scroll
function boton() {
// Posición del scroll vertical respecto al inicio de la página
let t = document.documentElement.scrollTop;
if (t > 200) {
// Si estamos al menos a 200px del inicio...
s.style.transform = "scale(1)"; // Visible
} else {
// Si no...
s.style.transform = "scale(0)"; // Oculto
}
}