switch
switch
HTML
<div class="center">
<input type="checkbox" value="1">
<input type="checkbox" value="2">
</div>
CSS
body {
margin: 0;
padding: 0;
background: #f3f3f3;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
input[type="checkbox"] {
width: 200px;
height: 100px;
-webkit-appearance: none;
-moz-appearance: none;
background: #c6c6c6;
outline: none;
border-radius: 50px;
box-shadow: inset 0 0 5px rgba(0,0,0, .2);
transition: 0.5s;
position: relative;
}
input:checked[type="checkbox"] {
background: #02a9f4;
}
input[type="checkbox"]::before {
content: '';
position: absolute;
width: 100px;
height: 100px;
border-radius: 50%;
top: 0;
left: 0;
background: #fff;
transform: scale(1.1);
box-shadow: 0 2px 5px rgba(0,0,0, .2);
transition: 0.5s;
}
input:checked[type="checkbox"]::before {
left: 100px;
}
JavaScript
const ff = document.querySelectorAll("input");
ff.forEach(el => {
el.onclick = function() {
if ((el.checked === true) && (el.value === "1")) {
document.body.style.backgroundColor = "#00ff00";
} else if ((el.checked === true) && (el.value === "2")) {
document.body.style.backgroundColor = "#ccff00";
} else {
document.body.style.backgroundColor = "#03f3f3";
}
};
})