MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
Line 121:
/* End of mw.loader.using callback */
} );
 
// Back to top (CapSach) — all skins; mobile widths
(function () {
var ID = 'cps-backtotop';
 
function ensureButton() {
if (document.getElementById(ID)) return;
 
var btn = document.createElement('button');
btn.id = ID;
btn.type = 'button';
btn.setAttribute('aria-label', 'Back to top');
btn.title = 'Back to top';
btn.innerHTML = '<span aria-hidden="true">↑</span>';
 
document.body.appendChild(btn);
updateVisibility();
 
var prefersReducedMotion = false;
try {
prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
} catch (e) {}
 
btn.addEventListener('click', function () {
try {
window.scrollTo({ top: 0, behavior: prefersReducedMotion ? 'auto' : 'smooth' });
} catch (e) {
// Older browsers
window.scrollTo(0, 0);
}
});
 
window.addEventListener('scroll', onScroll, { passive: true });
window.addEventListener('resize', onScroll, { passive: true });
}
 
var ticking = false;
function onScroll() {
if (!ticking) {
window.requestAnimationFrame(function () {
updateVisibility();
ticking = false;
});
ticking = true;
}
}
 
function updateVisibility() {
var show = window.pageYOffset > 300 &&
document.documentElement.scrollHeight > window.innerHeight * 2;
var el = document.getElementById(ID);
if (el) el.classList.toggle('is-visible', show);
}
 
// Run on initial load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', ensureButton);
} else {
ensureButton();
}
 
// Also run when MediaWiki re-initializes page content (preview, some SPA-ish flows)
if (window.mw && mw.hook) {
mw.hook('wikipage.content').add(ensureButton);
}
})();
 
/* DO NOT ADD CODE BELOW THIS LINE */