暗模式开关

问题描述:

我试图制作一个网站,当选择一个复选框时可以转到'暗模式'。我将复选框设计为看起来像一个开关。我使用一些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

尝试以下解决方法:

为了您的 '达累斯萨拉姆模式' 的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