MediaWiki:Common.js: Difference between revisions
Appearance
![]() Cdjensen94 (talk | contribs) |
![]() Cdjensen94 (talk | contribs) |
||
Line 1: | Line 1: | ||
(function() { | (function() { | ||
function | function resizeAndCenterSlideshow(mwSlideshow) { | ||
const slideshow = mwSlideshow.querySelector('.slideshow'); | const slideshow = mwSlideshow.querySelector('.slideshow'); | ||
if (!slideshow) return; | if (!slideshow) return; | ||
// | // Clear out any rogue inline styles | ||
slideshow.style.position = 'relative'; | slideshow.style.position = 'relative'; // Let it sit naturally inside mw-jsslideshow | ||
slideshow.style.left = '0'; | slideshow.style.left = '0'; | ||
slideshow.style.top = ' | slideshow.style.top = ''; | ||
slideshow.style.transform = ' | slideshow.style.transform = ''; | ||
// | // Make sure mw-jsslideshow fits around slideshow | ||
mwSlideshow.style.width = ' | mwSlideshow.style.width = slideshow.scrollWidth + 'px'; | ||
mwSlideshow.style.height = 'auto'; | mwSlideshow.style.height = slideshow.scrollHeight + 'px'; | ||
mwSlideshow.style.margin = '0 auto'; // Center the whole box horizontally | |||
// | // Center each individual image inside each slide | ||
slideshow.querySelectorAll('div').forEach(slide => { | slideshow.querySelectorAll('div').forEach(slide => { | ||
slide. | const img = slide.querySelector('img'); | ||
slide.style. | if (img) { | ||
// Make sure each slide is full-width & centers its image | |||
slide.style.display = 'flex'; | |||
slide.style.justifyContent = 'center'; | |||
slide.style.alignItems = 'center'; | |||
slide.style.width = slideshow.scrollWidth + 'px'; | |||
img.style.maxWidth = '100%'; | |||
img.style.height = 'auto'; | |||
img.style.objectFit = 'contain'; | |||
} | |||
}); | }); | ||
} | } | ||
function | function initSlideshowResizer() { | ||
document.querySelectorAll('.mw-jsslideshow').forEach(mwSlideshow => { | document.querySelectorAll('.mw-jsslideshow').forEach(mwSlideshow => { | ||
resizeAndCenterSlideshow(mwSlideshow); | |||
// | // Optional: Resize on window resize | ||
window.addEventListener('resize', () => { | window.addEventListener('resize', () => { | ||
resizeAndCenterSlideshow(mwSlideshow); | |||
}); | }); | ||
// Observe for changes (like slide swaps) | |||
const slideshow = mwSlideshow.querySelector('.slideshow'); | const slideshow = mwSlideshow.querySelector('.slideshow'); | ||
if (slideshow) { | if (slideshow) { | ||
const observer = new MutationObserver(() => { | const observer = new MutationObserver(() => { | ||
resizeAndCenterSlideshow(mwSlideshow); | |||
}); | }); | ||
observer.observe(slideshow, { | observer.observe(slideshow, { | ||
attributes: true, | attributes: true, | ||
attributeFilter: ['style'], | |||
childList: true, | childList: true, | ||
subtree: true | subtree: true | ||
Line 62: | Line 59: | ||
} | } | ||
// Init when ready | |||
if (document.readyState === 'complete' || document.readyState === 'interactive') { | if (document.readyState === 'complete' || document.readyState === 'interactive') { | ||
initSlideshowResizer(); | |||
} else { | } else { | ||
document.addEventListener('DOMContentLoaded', | document.addEventListener('DOMContentLoaded', initSlideshowResizer); | ||
} | } | ||