Widget:GF立绘切换:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 标签:手工回退 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
<includeonly><script> | <includeonly><script> | ||
(function() { | (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.switcherReady) return; | ||
box.dataset. | box.dataset.switcherReady = 'true'; | ||
var container = box.querySelector('.gf-image-container'); | var container = box.querySelector('.gf-image-container'); | ||
var | var thumbItems = box.querySelectorAll('.gf-thumb-item'); | ||
if (!container || | if (!container || thumbItems.length < 2) return; | ||
thumbItems.forEach(function(item) { | |||
item.addEventListener('click', function(e) { | |||
e.preventDefault(); | e.preventDefault(); | ||
// 更新激活状态 | // 更新激活状态 | ||
thumbItems.forEach(function(t) { t.classList.remove('active'); }); | |||
this.classList.add('active'); | this.classList.add('active'); | ||
// 获取新图片 | // 获取新图片文件名 | ||
var newFile = this.dataset.img; | var newFile = this.dataset.img; | ||
if (!newFile) return; | if (!newFile) return; | ||
// | // 获取旧图片 | ||
var oldImg = container.querySelector('img'); | var oldImg = container.querySelector('img'); | ||
// 创建 | // 创建新图片 | ||
var newImg = | var newImg = new Image(); | ||
newImg.style.maxWidth = '100%'; | newImg.style.maxWidth = '100%'; | ||
newImg.style.maxHeight = ' | newImg.style.maxHeight = '420px'; | ||
newImg.style.objectFit = 'contain'; | newImg.style.objectFit = 'contain'; | ||
newImg.style.opacity = '0'; | newImg.style.opacity = '0'; | ||
newImg.style.transition = 'opacity 0. | newImg.style.transition = 'opacity 0.35s ease'; | ||
newImg.onload = function() { | newImg.onload = function() { | ||
// 替换 | // 淡出旧图 | ||
container.innerHTML = ''; | if (oldImg) { | ||
oldImg.style.opacity = '0'; | |||
oldImg.style.transition = 'opacity 0.2s ease'; | |||
} | |||
}); | // 延迟替换 | ||
setTimeout(function() { | |||
container.innerHTML = ''; | |||
container.appendChild(newImg); | |||
requestAnimationFrame(function() { | |||
newImg.style.opacity = '1'; | |||
}); | |||
}, 200); | |||
}; | }; | ||
newImg.onerror = function() { | newImg.onerror = function() { | ||
// | console.warn('Failed to load image: ' + newFile); | ||
// 恢复旧图 | |||
if (oldImg) { | if (oldImg) { | ||
oldImg.style.opacity = '1'; | oldImg.style.opacity = '1'; | ||
} | } | ||
}; | }; | ||
// 使用 MediaWiki 文件路径 | |||
// 如果文件名不含 File: 前缀,尝试构建路径 | |||
if (newFile.startsWith('File:') || newFile.startsWith('文件:')) { | |||
newImg.src = newFile; | |||
} else { | |||
// 假设是 wiki 上的文件名 | |||
newImg.src = '/images/' + newFile; | |||
// 备用:直接当作完整URL | |||
newImg.onerror = function() { | |||
newImg.src = newFile; | |||
}; | |||
} | |||
}); | }); | ||
}); | }); | ||
| 第67行: | 第82行: | ||
} | } | ||
// | // 兼容 MediaWiki 动态加载 | ||
if (window.mw && mw.hook) { | if (window.mw && mw.hook) { | ||
mw.hook('wikipage.content').add(initSwitcher); | mw.hook('wikipage.content').add(initSwitcher); | ||