我試圖製作一個網站,當選擇一個複選框時可以轉到'暗模式'。我將複選框設計爲看起來像一個開關。我使用一些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 '' ;
}
爲什麼不,在功能'switch_style',或檢查複選框被選中沒有,並改變基於這樣的風格? – jonhopkins 2015-02-06 02:06:24
'switch_style'需要一個參數來說明啓用哪種樣式。因此,在你的點擊處理程序中,檢查複選框是否被選中,並用適當的標題調用函數。 – Barmar 2015-02-06 02:07:30
感謝您的幫助......您能演示如何做到這一點嗎?我有複製和粘貼該JavaScript代碼。我知道很多男中音JavaScript和多數民衆贊成在所有。謝謝@jonhopkins – 2015-02-06 02:08:37