Skip to content

Commit

Permalink
⚡️ Perf: 用 localStorage 储存 theme,代替 cookie #57
Browse files Browse the repository at this point in the history
  • Loading branch information
BigCoke233 committed May 30, 2020
1 parent d253a04 commit 244832e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions assets/js/miracles.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

/**
* Use cookies
* 快速使用 cookies
*/
//add cookie
function addCookie(name, value, expireHours) {
Expand Down Expand Up @@ -42,8 +42,17 @@ function getCookie(cname) {
}

/**
* Basic Animation
* 基础动画
* 快速使用 localStorage
*/
function setStrg(key, value) {
localStorage.setItem(key,value);
}
function getStrg(key) {
return localStorage.getItem(key);
}

/**
* 动画
*/
function scaleIn(object, time) {
object.css('transition', time).css('transform', 'scale(0)');
Expand Down Expand Up @@ -72,25 +81,24 @@ function gotoPosition(e){
var theme = 0;
function Dark() {
theme++;
//Add cookie to record
if(theme==2){
addCookie("theme", "2", 9);
setStrg("theme", "2");
$("#toggle-dark-button").html('<i class="iconfont icon-moon"></i>');
$("#toggle-dark-mobile").html('<i class="iconfont icon-moon"></i>');
$("body").removeClass("body-sepia");
$("body").addClass("body-dark");
alertSend("已切换夜间模式");
}
else if(theme==1){
addCookie("theme", "1", 9);
setStrg("theme", "1");
$("#toggle-dark-button").html('<i class="iconfont icon-sunset"></i>');
$("#toggle-dark-mobile").html('<i class="iconfont icon-sunset"></i>');
$("body").removeClass("body-dark");
$("body").addClass("body-sepia");
alertSend("已切换日落模式");
}
else {
addCookie("theme", "3", 9);
setStrg("theme", "3");
theme = 0;
$("#toggle-dark-button").html('<i class="iconfont icon-sun"></i>');
$("#toggle-dark-mobile").html('<i class="iconfont icon-sun"></i>');
Expand All @@ -101,7 +109,7 @@ function Dark() {
}

//auto dark theme
var whatTheme = getCookie("theme");
var whatTheme = getStrg("theme");
var schemeGeter = window.matchMedia ("(prefers-color-scheme: dark)");
if (whatTheme == "2" || schemeGeter.matches) {
$("body").addClass("body-dark");
Expand Down

0 comments on commit 244832e

Please sign in to comment.