MediaWiki:Common.js
MediaWiki界面页面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
<script>
// 以下代码应在 MediaWiki:Common.js 中加载
// 为了演示,这里直接嵌入;实际使用时请移至 Common.js
(function () {
'use strict';
// 仅在首页执行(检测页面名称)
var pageName = mw.config.get('wgPageName') || '';
if (pageName !== '首页' && pageName !== 'Main_Page') {
return;
}
// ---------- 1. 欢迎弹窗 ----------
var popup = document.getElementById('hpWelcomePopup');
var closeBtn = document.getElementById('hpClosePopupBtn');
var dontShowCheck = document.getElementById('hpDontShowAgain');
if (popup && closeBtn) {
// 检查 localStorage
var noShow = localStorage.getItem('hpWikiWelcomeNoShow') === 'true';
if (noShow) {
popup.style.display = 'none';
}
function closePopup() {
popup.style.display = 'none';
if (dontShowCheck && dontShowCheck.checked) {
localStorage.setItem('hpWikiWelcomeNoShow', 'true');
}
}
closeBtn.addEventListener('click', closePopup);
}
// ---------- 2. 滚动渐显 (Intersection Observer) ----------
var cards = document.querySelectorAll('.hp-card');
if ('IntersectionObserver' in window && cards.length) {
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
var card = entry.target;
var delay = parseInt(card.getAttribute('data-delay')) || 0;
setTimeout(function () {
card.classList.add('hp-visible');
}, delay);
observer.unobserve(card);
}
});
}, {
threshold: 0.15,
rootMargin: '0px 0px -30px 0px'
});
cards.forEach(function (card) {
observer.observe(card);
});
} else {
// 降级:直接显示所有卡片
cards.forEach(function (card) {
card.classList.add('hp-visible');
});
}
})();
</script>
```