HTML代码示例:
CSS代码示例:
.switch input[type="checkbox"] {
opacity: 0;
position: absolute;
pointer-events: none;
}
.switch .slider {
background-color: grey;
border-radius: 30px;
cursor: pointer;
display: inline-block;
height: 18px;
margin-right: 10px;
position: relative;
transition: background-color 0.2s ease;
width: 34px;
}
.switch input[type="checkbox"]:checked + .slider {
background-color: green;
}
JavaScript代码示例:
var colorSwitch = document.querySelector("#colorSwitch");
colorSwitch.addEventListener("change", function() {
if (this.checked) {
document.body.style.backgroundColor = "green";
} else {
document.body.style.backgroundColor = "white";
}
});
如果以上步骤没有解决问题,请检查浏览器控制台中是否有错误消息,或者尝试使用浏览器的开发工具进行代码调试。
上一篇:背景颜色高度
下一篇:背景颜色更改无法实现