2015-02-06 67 views
1

我試圖製作一個網站,當選擇一個複選框時可以轉到'暗模式'。我將複選框設計爲看起來像一個開關。我使用一些JavaScript來加載替代樣式表'onChange'。這工作正常,問題是,當您取消選中該框時,我想再次加載原始樣式表。任何幫助將不勝感激。我是JavaScript新手,想了解更多信息。以下是我的HTML,CSS & Javascript代碼:暗模式開關

<input type="checkbox" id="switch1" name="switch1" class="switch" onchange="switch_style('alt');"/> 
<label for="switch1">Dark Mode</label> 


/* This is for the dark mode switch */ 
input.switch:empty 
{ 
    margin-left: -999px; 
} 
input.switch:empty ~ label 
{ 
    position: relative; 
    float: left; 
    line-height: 1.6em; 
    text-indent: 4em; 
    margin: 0.2em 0; 
    cursor: pointer; 
    -webkit-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
} 
input.switch:empty ~ label:before, 
input.switch:empty ~ label:after 
{ 
    position: absolute; 
    display: block; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    content: ' '; 
    width: 3.6em; 
    background-color: #c33; 
    border-radius: 0.3em; 
    box-shadow: inset 0 0.2em 0 rgba(0,0,0,0.3); 
    -webkit-transition: all 100ms ease-in; 
    transition: all 100ms ease-in; 
} 

input.switch:empty ~ label:after 
{ 
    width: 1.4em; 
    top: 0.1em; 
    bottom: 0.1em; 
    margin-left: 0.1em; 
    background-color: #fff; 
    border-radius: 0.15em; 
    box-shadow: inset 0 -0.2em 0 rgba(0,0,0,0.2); 
} 
input.switch:checked ~ label:before 
{ 
    background-color: #393; 
} 

input.switch:checked ~ label:after 
{ 
    margin-left: 2em; 
} 

// *** TO BE CUSTOMISED *** 

var style_cookie_name = "style" ; 
var style_cookie_duration = 30 ; 

// *** END OF CUSTOMISABLE SECTION *** 
// You do not need to customise anything below this line 

function switch_style (css_title) 
{ 
// You may use this script on your site free of charge provided 
// you do not remove this notice or the URL below. Script from 
// http://www.thesitewizard.com/javascripts/change-style-sheets.shtml 
    var i, link_tag ; 
    for (i = 0, link_tag = document.getElementsByTagName("link") ; 
    i < link_tag.length ; i++) { 
    if ((link_tag[i].rel.indexOf("stylesheet") != -1) && 
     link_tag[i].title) { 
     link_tag[i].disabled = true ; 
     if (link_tag[i].title == css_title) { 
     link_tag[i].disabled = false ; 
     } 
    } 
    set_cookie(style_cookie_name, css_title, 
     style_cookie_duration); 
    } 
} 
function set_style_from_cookie() 
{ 
    var css_title = get_cookie(style_cookie_name); 
    if (css_title.length) { 
    switch_style(css_title); 
    } 
} 
function set_cookie (cookie_name, cookie_value, 
    lifespan_in_days, valid_domain) 
{ 
    // http://www.thesitewizard.com/javascripts/cookies.shtml 
    var domain_string = valid_domain ? 
         ("; domain=" + valid_domain) : '' ; 
    document.cookie = cookie_name + 
         "=" + encodeURIComponent(cookie_value) + 
         "; max-age=" + 60 * 60 * 
         24 * lifespan_in_days + 
         "; path=/" + domain_string ; 
} 
function get_cookie (cookie_name) 
{ 
    // http://www.thesitewizard.com/javascripts/cookies.shtml 
    var cookie_string = document.cookie ; 
    if (cookie_string.length != 0) { 
     var cookie_value = cookie_string.match (
         '(^|;)[\s]*' + 
         cookie_name + 
         '=([^;]*)'); 
     return decodeURIComponent (cookie_value[2]) ; 
    } 
    return '' ; 
} 
+1

爲什麼不,在功能'switch_style',或檢查複選框被選中沒有,並改變基於這樣的風格? – jonhopkins 2015-02-06 02:06:24

+0

'switch_style'需要一個參數來說明啓用哪種樣式。因此,在你的點擊處理程序中,檢查複選框是否被選中,並用適當的標題調用函數。 – Barmar 2015-02-06 02:07:30

+0

感謝您的幫助......您能演示如何做到這一點嗎?我有複製和粘貼該JavaScript代碼。我知道很多男中音JavaScript和多數民衆贊成在所有。謝謝@jonhopkins – 2015-02-06 02:08:37

回答

5

嘗試以下解決方法:

爲了您的 '達累斯薩拉姆模式' 的CSS,每個選擇前添加dark-mode類。

.dark-mode input { } 
.dark-mode input.switch:empty ~ label { } 

等等

然後在你的JS,只需撥動類 '暗模式' 的身體。 我認爲這種方式可能更有效。

試試這個類似的演示:

http://jsfiddle.net/7Lfv0jqL/

+0

對不起......我不明白......你能詳細說明一下嗎?謝謝@Jayson Buquia – 2015-02-06 02:19:07

+0

您的'暗模式'(下面的樣式/ *這是暗模式開關* /)的所有樣式,在它們之前添加'.dark-mode'。然後在你的JS中,當複選框被選中時,使用'$('。body')。addClass('dark-mode')',然後'$('。body')。addClass('dark-mode')'當複選框沒有被選中時。 – qtgye 2015-02-06 02:26:26

+0

對不起,如果這是一個屁股太多,那麼不要擔心,但你能幫我扭轉JS代碼嗎?我不知道該怎麼做。我相信有一個更簡單的方法來做到這一點,因爲我複製和粘貼的代碼將其製作成cookie和所有這些巨型珍寶。或者你能幫我編輯現有的代碼嗎?謝謝你很多@Jayson Buquia – 2015-02-06 02:34:26