Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display different div element components when Toggle switch slider

I want to have a toggle switch which if slide left a div content components pops up and on slide right a different div content pop up.How can I proceed?

HTML CONTENT

<form>
    <label class="switch">
        <input type="checkbox" checked="true" />
        <div class="slider round"></div>
    </label>
</form>

<div id ="menu1">
    Menu 1 content
</div>
<div id ="menu2">
    Menu 2 content
</div>

CSS CONTENT

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

As you can see I want like if the toggle slides right content of div id="menu1" to be displayed and on slide left content of div id="menu2" be displayed.I want to have a toggle switch which if slide left a div content components pops up and on slide right a different div content pop up.How can I proceed?

like image 953
Usha Some Avatar asked Aug 18 '26 08:08

Usha Some


1 Answers

This should do it but you'll need jQuery. Basically, we hide #menu2 with CSS as display:none; as default and then use JS to .show or .hide for each. Let me know if you have questions.

$(function() {
  $("#toggle").click(function() {
    if ($(this).is(":checked")) {
      $("#menu1").show();
      $("#menu2").hide();
    } else {
      $("#menu1").hide();
      $("#menu2").show();
    }
  });
});
/* The switch - the box around the slider */

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}


/* Hide default HTML checkbox */

.switch input {
  display: none;
}


/* The slider */

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

#menu2 {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label class="switch">
        <input type="checkbox" id="toggle" checked="true" />
        <div class="slider round"></div>
    </label>
</form>

<div id="menu1">
  Menu 1 content
</div>
<div id="menu2">
  Menu 2 content
</div>
like image 99
Darren Avatar answered Aug 20 '26 00:08

Darren