Differenze tra le versioni di "MediaWiki:Common.js"
(Creata pagina con "→Il codice JavaScript inserito qui viene caricato da ciascuna pagina, per tutti gli utenti.: document.addEventListener("DOMContentLoaded", function() { let headerHeig...") |
|||
| Riga 2: | Riga 2: | ||
document.addEventListener("DOMContentLoaded", function() { | document.addEventListener("DOMContentLoaded", function() { | ||
| − | let headerHeight = document.querySelector("mw-header-container").offsetHeight; | + | // Prende l'altezza dell'header |
| + | let headerHeight = document.querySelector("mw-header-container").offsetHeight; | ||
document.querySelectorAll('a[href^="#"]').forEach(anchor => { | document.querySelectorAll('a[href^="#"]').forEach(anchor => { | ||
anchor.addEventListener("click", function(e) { | anchor.addEventListener("click", function(e) { | ||
| Riga 8: | Riga 9: | ||
let target = document.querySelector(this.getAttribute("href")); | let target = document.querySelector(this.getAttribute("href")); | ||
if (target) { | if (target) { | ||
| − | + | // Aggiunge uno spazio extra dell'immagine e dei padding | |
| + | let targetPosition = target.offsetTop - headerHeight + 500; | ||
window.scrollTo({ top: targetPosition, behavior: "smooth" }); | window.scrollTo({ top: targetPosition, behavior: "smooth" }); | ||
} | } | ||
Versione delle 08:31, 16 gen 2025
/* Il codice JavaScript inserito qui viene caricato da ciascuna pagina, per tutti gli utenti. */
document.addEventListener("DOMContentLoaded", function() {
// Prende l'altezza dell'header
let headerHeight = document.querySelector("mw-header-container").offsetHeight;
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener("click", function(e) {
e.preventDefault();
let target = document.querySelector(this.getAttribute("href"));
if (target) {
// Aggiunge uno spazio extra dell'immagine e dei padding
let targetPosition = target.offsetTop - headerHeight + 500;
window.scrollTo({ top: targetPosition, behavior: "smooth" });
}
});
});
});