Widget:GF立绘切换:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
(function() { | <script> | ||
(function () { | |||
function initSwitcher() { | function initSwitcher() { | ||
document.querySelectorAll('.gf-infobox-new').forEach(function(box) { | document.querySelectorAll('.gf-infobox-new').forEach(function (box) { | ||
if (box.dataset. | if (box.dataset.ready) return; | ||
box.dataset. | box.dataset.ready = '1'; | ||
var container = box.querySelector('.gf-image-container'); | var container = box.querySelector('.gf-image-container'); | ||
var | var thumbs = box.querySelectorAll('.gf-thumb-item'); | ||
if (!container || thumbs.length < 2) return; | |||
if ( | thumbs.forEach(function (thumb) { | ||
thumb.addEventListener('click', function () { | |||
if (thumb.classList.contains('active')) return; | |||
var src = thumb.dataset.src; | |||
if (!src) return; | |||
if ( | |||
// 淡出旧图 | |||
var oldImg = container.querySelector('img'); | |||
// 淡出 | |||
var oldImg = container.querySelector(' | |||
if (oldImg) { | if (oldImg) { | ||
oldImg.style.transition = 'opacity 0.15s | oldImg.style.transition = 'opacity 0.15s'; | ||
oldImg.style.opacity = '0'; | oldImg.style.opacity = '0'; | ||
} | } | ||
setTimeout(function() { | setTimeout(function () { | ||
// | // 直接替换为 <img>,不依赖 wiki 语法 | ||
container.innerHTML = '<img src="' + | container.innerHTML = '<img src="' + src + '" style="max-width:100%;max-height:420px;object-fit:contain;opacity:0;transition:opacity 0.3s;">'; | ||
var newImg = container.querySelector('img'); | var newImg = container.querySelector('img'); | ||
if (newImg) { | if (newImg) { | ||
// 图片加载完再淡入 | |||
newImg.onload = function () { | |||
newImg.style.opacity = '1'; | newImg.style.opacity = '1'; | ||
}; | |||
// 已缓存时 onload 不触发,用 rAF 兜底 | |||
requestAnimationFrame(function () { | |||
if (newImg.complete) newImg.style.opacity = '1'; | |||
}); | }); | ||
} | } | ||
}, 150); | }, 150); | ||
thumbs.forEach(function (t) { t.classList.remove('active'); }); | |||
thumb.classList.add('active'); | |||
}); | }); | ||
}); | }); | ||
| 第43行: | 第47行: | ||
} | } | ||
// 三重保险确保 DOM 就绪后再初始化 | |||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', initSwitcher); | document.addEventListener('DOMContentLoaded', initSwitcher); | ||
| 第49行: | 第54行: | ||
} | } | ||
if (window.mw | // MediaWiki 的标准钩子,处理动态加载内容 | ||
if (window.mw) { | |||
mw.hook('wikipage.content').add(initSwitcher); | mw.hook('wikipage.content').add(initSwitcher); | ||
} | } | ||
})(); | })(); | ||
</script> | |||
2026年5月15日 (五) 20:53的版本
<script> (function () {
function initSwitcher() {
document.querySelectorAll('.gf-infobox-new').forEach(function (box) {
if (box.dataset.ready) return;
box.dataset.ready = '1';
var container = box.querySelector('.gf-image-container');
var thumbs = box.querySelectorAll('.gf-thumb-item');
if (!container || thumbs.length < 2) return;
thumbs.forEach(function (thumb) {
thumb.addEventListener('click', function () {
if (thumb.classList.contains('active')) return;
var src = thumb.dataset.src;
if (!src) return;
// 淡出旧图
var oldImg = container.querySelector('img');
if (oldImg) {
oldImg.style.transition = 'opacity 0.15s';
oldImg.style.opacity = '0';
}
setTimeout(function () {
// 直接替换为 <img>,不依赖 wiki 语法
container.innerHTML = '<img src="' + src + '" style="max-width:100%;max-height:420px;object-fit:contain;opacity:0;transition:opacity 0.3s;">';
var newImg = container.querySelector('img');
if (newImg) {
// 图片加载完再淡入
newImg.onload = function () {
newImg.style.opacity = '1';
};
// 已缓存时 onload 不触发,用 rAF 兜底
requestAnimationFrame(function () {
if (newImg.complete) newImg.style.opacity = '1';
});
}
}, 150);
thumbs.forEach(function (t) { t.classList.remove('active'); });
thumb.classList.add('active');
});
});
});
}
// 三重保险确保 DOM 就绪后再初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initSwitcher);
} else {
initSwitcher();
}
// MediaWiki 的标准钩子,处理动态加载内容
if (window.mw) {
mw.hook('wikipage.content').add(initSwitcher);
}
})(); </script>