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 inputs = [...document.querySelectorAll("input")]
const colors = {
"0": "#03f3f3",
"1": "#00ff00",
"2": "#ccff00"
}
inputs.forEach(input => {
input.addEventListener('click', () => {
document.body.style.backgroundColor = inputs.some(i => i.checked)
? colors[input.value]
: colors["0"]
})
})