mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-15 02:24:04 +00:00
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
/*
|
|
* Citizen - ToC JS
|
|
* https://starcitizen.tools
|
|
*
|
|
* Smooth scroll fallback and Scrollspy
|
|
*/
|
|
|
|
const SmoothScroll = () => {
|
|
if (!'scrollBehavior' in document.documentElement.style) {
|
|
const navLinks = document.querySelectorAll('#toc a');
|
|
|
|
for (let n in navLinks) {
|
|
if (navLinks.hasOwnProperty(n)) {
|
|
navLinks[n].addEventListener('click', e => {
|
|
e.preventDefault();
|
|
document.querySelector(navLinks[n].hash)
|
|
.scrollIntoView({
|
|
behavior: "smooth"
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const ScrollSpy = () => {
|
|
const sections = document.querySelectorAll('.mw-headline');
|
|
|
|
window.onscroll = () => {
|
|
const scrollPos = document.documentElement.scrollTop || document.body.scrollTop;
|
|
|
|
for (let s in sections)
|
|
if (sections.hasOwnProperty(s) && sections[s].offsetTop <= scrollPos) {
|
|
const id = mw.util.escapeIdForAttribute(sections[s].id);
|
|
document.querySelector('.active').classList.remove('active');
|
|
document.querySelector(`a[href*="${ id }"]`).parentNode.classList.add('active');
|
|
}
|
|
}
|
|
}
|
|
|
|
const CheckToC = () => {
|
|
if (document.getElementById("toc")) {
|
|
SmoothScroll();
|
|
ScrollSpy();
|
|
}
|
|
}
|
|
|
|
if (document.readyState !== 'loading') {
|
|
CheckToC();
|
|
} else if (document.addEventListener) {
|
|
// All modern browsers to register DOMContentLoaded
|
|
document.addEventListener('DOMContentLoaded', CheckToC);
|
|
} else {
|
|
// Old IE browsers
|
|
document.attachEvent('onreadystatechange', function() {
|
|
if (document.readyState === 'complete') {
|
|
CheckToC();
|
|
}
|
|
});
|
|
} |