How do I make a switch button to switch css?

2

I have two CSS files for the same HTML and I would like to use a switch button to switch styles. The idea is to set up a switch "default layout" "new layout".

I tried this, but I did not succeed.

<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"  id="original"></span>
        <span class="onoffswitch-switch"  id="novo"></span>
    </label>
</div>

and also

$('#novo').click(function (){
  $('link[href="theme_4_1.css"]').attr('href','theme_4_1.css');
});
$('#original').click(function (){
   $('link[href="theme_4_0.css"]').attr('href','theme_4_0.css');
});
    
asked by anonymous 10.03.2017 / 13:53

3 answers

3

I think this is because href is converted to absolute address, so you can make the selector like href$="theme_4_0.css" , otherwise I believe you have reversed the selectors, I think the correct one is like this:

$('#novo').click(function (){
  $('link[href$="theme_4_0.css"]').attr('href', 'theme_4_1.css');
});
$('#original').click(function (){
   $('link[href$="theme_4_1.css"]').attr('href', 'theme_4_0.css');
});

Or it may be that you applied the event in SPAN but both are triggered because they are inside the switch, so change to this:

$('#myonoffswitch').change(function () {
     if (this.checked) { //Se ON aplica o novo tema
         $('link[href$="theme_4_0.css"]').attr('href', 'theme_4_1.css');
     } else { //Se OFF aplica o tema antigo
         $('link[href$="theme_4_1.css"]').attr('href', 'theme_4_0.css');
     }
});

Using disabled with link

You can also create 2 link elements directly in HTML (I believe you are using this script link ):

$(function() {
    var tema1 = $("#theme-4.0");
    var tema2 = $("#theme-4.1");
    
    $('#TROCAR-TEMA').change(function () {
        if (this.checked) {
            console.log("Tema novo");
            tema1.prop("disabled", true);
            tema2.prop("disabled", false);
        } else {
            console.log("Tema antigo");
            tema1.prop("disabled", false);
            tema2.prop("disabled", true);
        }
    });
});
.onoffswitch {
    position: relative; width: 90px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 6px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 56px;
    border: 2px solid #999999; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkid="theme-4.0" rel="stylesheet" href="tema1.css">
<link id="theme-4.1" rel="stylesheet" href="tema2.css" disabled>

<div class="container">
  <div class="onoffswitch">
      <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="TROCAR-TEMA" checked>
      <label class="onoffswitch-label" for="TROCAR-TEMA">
          <span class="onoffswitch-inner"></span>
          <span class="onoffswitch-switch"></span>
      </label>
  </div>
</div>
    
10.03.2017 / 17:47
0

In my projects I use the following code and it works well:

Html:

<link id="theme" href="dark.css" rel="stylesheet">
<button id="whiteTheme" data-theme="white.css">White Theme</button>


Script:

$("button[data-theme]").click(function() {
    $("link#theme").attr("href", $(this).data("theme"));
});

I hope it helps.

    
10.03.2017 / 14:26
0

Solved the problem by reversing the order of href.

$('#novo').click(function (){
    $('link[href="theme_4_1.css"]').attr('href','theme_4_0.css');
});
$('#original').click(function (){
    $('link[href="theme_4_0.css"]').attr('href','theme_4_1.css');
});
    
10.03.2017 / 15:15