MediaWiki:Mobile.js: Difference between revisions
Appearance
Content deleted Content added
Created page with "→All JavaScript here will be loaded for users of the mobile site: // 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>';..." |
Replaced content with "→All JavaScript here will be loaded for users of the mobile site: " Tag: Replaced |
||
| Line 1: | Line 1: | ||
/* All JavaScript here will be loaded for users of the mobile site */ |
/* All JavaScript here will be loaded for users of the mobile site */ |
||
// 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); |
|||
} |
|||
})(); |
|||