javascript - 深色模式开关

标签 javascript html css

我正在尝试制作一个可以在选中复选框时转为“暗模式”的网站。我已将复选框设置为看起来像一个开关。我让它与一些 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 '' ;
}

最佳答案

试试这个解决方法:

对于“Dar Mode”css,在每个选择器之前添加 dark-mode 类。
即,

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

等...

然后在您的 JS 中,只需为主体切换类“暗模式”。 我认为那样可能更有效。

试试这个类似的演示:

http://jsfiddle.net/7Lfv0jqL/

关于javascript - 深色模式开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357405/

相关文章:

javascript - 使用 JavaScript 的事件委托(delegate)而不是 jQuery 有性能优势吗?

jquery - 试图将我的 UL 的一部分向左移动

html - 第 n 个子 ul 列表项背景重复到子 ul 列表

html - 不需要的填充找不到问题

html - 有没有一种简单的方法可以确保 .well 元素无论屏幕大小如何都具有相同的高度和宽度?

javascript - javascript(钛)中的PDF页数

javascript - 使用 jquery 或 CSS 向左滑动隐藏效果

javascript - 在 chrome 开发控制台中显示重复的 ID

html - 在全高列中垂直对齐图像

html - 如何隐藏特定标题下的表格列?