68 lines
2.4 KiB
JavaScript
68 lines
2.4 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 | ?only=home —— 只显示某一演示区(方便单独截图/对比) */
|
|
(function () {
|
|
var params = new URLSearchParams(location.search);
|
|
var only = params.get('only');
|
|
if (!only) return;
|
|
var id = { login: 'login-demo', home: 'landing-demo' }[only];
|
|
if (!id) return;
|
|
var page = document.querySelector('.page');
|
|
var keep = document.getElementById(id);
|
|
if (!page || !keep) return;
|
|
var label = keep.previousElementSibling;
|
|
Array.prototype.forEach.call(page.children, function (el) {
|
|
if (el === keep || el === label) return;
|
|
el.style.display = 'none';
|
|
});
|
|
if (only === 'home') {
|
|
/* 让离线预览也按线上 1280px 的容器宽度呈现 */
|
|
page.style.maxWidth = '1320px';
|
|
page.style.paddingTop = '0';
|
|
keep.style.border = '0';
|
|
keep.style.background = 'transparent';
|
|
keep.style.borderRadius = '0';
|
|
if (label) label.style.display = 'none';
|
|
if (params.get('bare') === '1') {
|
|
/* 截图用:预览器自己的导航会撑宽小屏,隐藏它才能看到真实 375px 版式 */
|
|
page.style.padding = '0';
|
|
page.style.maxWidth = 'none';
|
|
['#navbar', '.switcher'].forEach(function (sel) {
|
|
var el = document.querySelector(sel);
|
|
if (el) el.style.display = 'none';
|
|
});
|
|
}
|
|
}
|
|
})();
|