47 lines
1.7 KiB
JavaScript
47 lines
1.7 KiB
JavaScript
/* 预览页的主题切换:只切换三份主题 CSS 的启用状态,不改任何标记 */
|
|
(function () {
|
|
const links = {
|
|
auto: document.getElementById('css-auto'),
|
|
light: document.getElementById('css-light'),
|
|
dark: document.getElementById('css-dark'),
|
|
};
|
|
const buttons = Array.from(document.querySelectorAll('.switcher button'));
|
|
|
|
function apply(mode) {
|
|
Object.keys(links).forEach((key) => {
|
|
if (links[key]) links[key].disabled = key !== mode;
|
|
});
|
|
document.documentElement.dataset.theme = mode;
|
|
buttons.forEach((b) => b.classList.toggle('on', b.dataset.mode === mode));
|
|
try { localStorage.setItem('qq-preview-theme', mode); } catch (e) { /* file:// 下可能不可用 */ }
|
|
}
|
|
|
|
buttons.forEach((b) => b.addEventListener('click', () => apply(b.dataset.mode)));
|
|
|
|
/* 支持 ?theme=light|dark|auto 直接进入某档,方便截图与分享 */
|
|
const forced = new URLSearchParams(location.search).get('theme');
|
|
if (forced && links[forced]) {
|
|
apply(forced);
|
|
return;
|
|
}
|
|
|
|
let saved = null;
|
|
try { saved = localStorage.getItem('qq-preview-theme'); } catch (e) { /* ignore */ }
|
|
apply(saved && links[saved] ? saved : 'auto');
|
|
})();
|
|
|
|
|
|
/* ?only=login —— 只显示登录页演示区(方便单独截图/对比) */
|
|
(function () {
|
|
var only = new URLSearchParams(location.search).get('only');
|
|
if (only !== 'login') return;
|
|
var page = document.querySelector('.page');
|
|
if (!page) return;
|
|
var keep = document.getElementById('login-demo');
|
|
var label = keep && keep.previousElementSibling;
|
|
Array.prototype.forEach.call(page.children, function (el) {
|
|
if (el === keep || el === label) return;
|
|
el.style.display = 'none';
|
|
});
|
|
})();
|