From f19cf4dd61d579b221a6ac4a88a3f337e8e014a7 Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 7 Jul 2024 08:15:40 +0800 Subject: [PATCH] remember css style --- www/js/beauty.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/www/js/beauty.js b/www/js/beauty.js index 0444ec1..7955eed 100644 --- a/www/js/beauty.js +++ b/www/js/beauty.js @@ -67,21 +67,46 @@ if ($('#image_site').get(0)) { // 白天黑夜模式切换 - var lanpnum = 0; - $('#image_site .lampJS').click(function () { - if (lanpnum == 0) { + var saveLanpnumToLocalstorage = function(lanpnum) { + try { + var key = 'user_lanpnum'; + localStorage.setItem(key, lanpnum); + }catch(err) { + console.error('保存本地存储失败', err); + } + }; + + var getLanpnumFromLocalstorage = function() { + try { + var key = 'user_lanpnum'; + return localStorage.getItem(key); + }catch(err) { + console.error('保存本地存储失败', err); + } + + return false; + }; + + var toggleLampshow = function(lanpnum) { + if (lanpnum == 1) { $('#markdowncss').attr('href', '/css/github-markdown-dark.css'); $(document.body).addClass('lampshow'); $('#image_site .navbarJS').removeClass('navbar-default').addClass('navbar-inverse'); // 导航栏用bootstrap主题切换 - lanpnum = 1; - } else if (lanpnum == 1) { + } else if (lanpnum == 0) { $('#markdowncss').attr('href', '/css/github-markdown-light.css'); $(document.body).removeClass('lampshow'); $('#image_site .navbarJS').addClass('navbar-default').removeClass('navbar-inverse'); - lanpnum = 0; } + }; - return; + var lanpnum = getLanpnumFromLocalstorage(); + if (lanpnum !== false) { + toggleLampshow(lanpnum); + } + $('#image_site .lampJS').click(function () { + lanpnum = lanpnum == 0 ? 1 : 0; + toggleLampshow(lanpnum); + saveLanpnumToLocalstorage(lanpnum); }); // 音乐播放