feat(樱花特效): 添加本地存储功能以保存用户偏好
添加 localStorage 支持来持久化樱花特效的显示状态 修复首次加载时特效状态未正确应用的问题
This commit is contained in:
parent
0a2cf047b4
commit
0ba2caea01
@ -1,4 +1,27 @@
|
||||
// 樱花特效切换按钮 - 添加到 rightside 设置区域
|
||||
const SAKURA_STORAGE_KEY = 'sakura_effect_enabled';
|
||||
|
||||
// 获取保存的状态,默认为 true(显示)
|
||||
function getSakuraState() {
|
||||
const saved = localStorage.getItem(SAKURA_STORAGE_KEY);
|
||||
// 如果没有保存过,默认显示
|
||||
return saved === null ? true : saved === 'true';
|
||||
}
|
||||
|
||||
// 保存状态到 localStorage
|
||||
function saveSakuraState(enabled) {
|
||||
localStorage.setItem(SAKURA_STORAGE_KEY, enabled.toString());
|
||||
}
|
||||
|
||||
// 应用樱花特效状态
|
||||
function applySakuraState() {
|
||||
const sakuraEffect = document.getElementById('canvas_sakura');
|
||||
if (sakuraEffect) {
|
||||
const enabled = getSakuraState();
|
||||
sakuraEffect.style.display = enabled ? 'block' : 'none';
|
||||
}
|
||||
}
|
||||
|
||||
function createSakuraBtn() {
|
||||
// 检查按钮是否已存在
|
||||
if (document.getElementById('toggle_sakura_btn')) return;
|
||||
@ -14,18 +37,31 @@ function createSakuraBtn() {
|
||||
hideArea.appendChild(sakuraBtn);
|
||||
}
|
||||
|
||||
// 应用保存的状态
|
||||
applySakuraState();
|
||||
|
||||
// 点击切换
|
||||
sakuraBtn.addEventListener('click', function() {
|
||||
const sakuraEffect = document.getElementById('canvas_sakura');
|
||||
if (sakuraEffect) {
|
||||
const isHidden = window.getComputedStyle(sakuraEffect).display === 'none';
|
||||
sakuraEffect.style.display = isHidden ? 'block' : 'none';
|
||||
const newState = isHidden ? true : false;
|
||||
sakuraEffect.style.display = newState ? 'block' : 'none';
|
||||
// 保存状态到 localStorage
|
||||
saveSakuraState(newState);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 首次加载
|
||||
document.addEventListener('DOMContentLoaded', createSakuraBtn);
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
createSakuraBtn();
|
||||
// 确保 sakura.js 加载完成后应用状态
|
||||
setTimeout(applySakuraState, 100);
|
||||
});
|
||||
|
||||
// PJAX 页面切换后重新创建
|
||||
document.addEventListener('pjax:complete', createSakuraBtn);
|
||||
document.addEventListener('pjax:complete', function() {
|
||||
createSakuraBtn();
|
||||
applySakuraState();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user