1- -- -
2- layout : null
3- -- -
4- var darkTheme = document . createElement ( "link" ) ;
5- darkTheme . rel = "stylesheet alternate" ;
6- darkTheme . href = "{{ '/assets/css/dark.css' | relative_url }}" ;
7- document . head . appendChild ( darkTheme ) ;
8- window . addEventListener ( "DOMContentLoaded" , function ( ) {
9- var list = document . querySelector ( ".masthead .visible-links" ) ;
10- if ( ! list ) return ;
11- var mediaQuery = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
12- function handler ( ) {
13- darkTheme . rel = mediaQuery . matches ? "stylesheet" : "stylesheet alternate" ;
14- }
15- var current = 0 ;
16- var modes = [ "light" , "dark" , "auto" ] ;
17- var modeNames = [ "亮色" , "暗色" , "自动" ] ;
18- var switcher = document . createElement ( "a" ) ;
19- switcher . className = "masthead__menu-item" ;
20- switcher . innerText = modeNames [ current ] ;
21- switcher . href = "javascript:;" ;
22- switcher . onclick = function ( ) {
23- themeApply ( current + 1 ) ;
24- }
25- list . appendChild ( switcher ) ;
26- function themeApply ( index ) {
27- index = ( Number ( index ) || 0 ) % modes . length ;
28- if ( index === current ) return ;
29- if ( modes [ current ] === "auto" ) mediaQuery . removeEventListener ( "change" , handler ) ;
30- current = index ;
31- var mode = modes [ current ] ;
32- switcher . innerText = modeNames [ current ] ;
33- localStorage . setItem ( "theme" , current ) ;
34- if ( mode === "light" ) darkTheme . rel = "stylesheet alternate" ;
35- else if ( mode === "dark" ) darkTheme . rel = "stylesheet" ;
36- else {
37- mediaQuery . addEventListener ( "change" , handler ) ;
38- handler ( ) ;
39- }
40- }
41- themeApply ( localStorage . getItem ( "theme" ) ) ;
42- window . addEventListener ( "storage" , function ( event ) {
43- event . key === "theme" && themeApply ( event . newValue ) ;
44- } ) ;
45- } ) ;
1+ $ ( function ( ) {
2+ var $alt = $ ( "#theme-color-alternate" ) , $style = $ ( "#theme-color-style" ) ;
3+ var current = 0 , modes = [ "light" , "dark" , "auto" ] , mq = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
4+ function applyTheme ( idx ) {
5+ idx = ( Number ( idx ) || 0 ) % modes . length ;
6+ if ( idx === current ) return ;
7+ if ( modes [ current ] === "auto" ) mq . removeEventListener ( "change" , handler ) ;
8+ current = idx ;
9+ var mode = modes [ current ] ;
10+ $style . html ( ".theme-color-switcher.theme-color-" + mode + "{display:block!important}" ) ;
11+ localStorage . setItem ( "theme" , current ) ;
12+ if ( mode === "light" ) $alt . attr ( "rel" , "stylesheet alternate" ) ;
13+ else if ( mode === "dark" ) $alt . attr ( "rel" , "stylesheet" ) ;
14+ else { mq . addEventListener ( "change" , handler ) ; handler ( ) ; }
15+ }
16+ function handler ( ) { $alt . attr ( "rel" , mq . matches ? "stylesheet" : "stylesheet alternate" ) ; }
17+ $ ( ".theme-color-switcher" ) . on ( "click" , function ( ) { applyTheme ( current + 1 ) ; } ) ;
18+ applyTheme ( localStorage . getItem ( "theme" ) ) ;
19+ $ ( window ) . on ( "storage" , e => e . originalEvent . key === "theme" && applyTheme ( e . originalEvent . newValue ) ) ;
20+ } ) ;
0 commit comments