<!-- HTML Switch 示例代码 -->
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
<style>
/* 定义开关的样式 */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* 隐藏默认的复选框 */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* 创建滑块 */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
/* 当复选框被选中时改变滑块的颜色和位置 */
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
/* 圆形滑块 */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
HTML部分:
<label>
标签包裹一个复选框<input type="checkbox">
和一个空的<span>
元素。class="switch"
用于应用CSS样式。CSS部分:
.switch
类定义了整个开关的容器大小和布局。.switch input
隐藏了默认的复选框,使其不可见。.slider
类创建了一个滑块,并设置了初始背景颜色和过渡效果。.slider:before
伪元素用于创建滑块中的圆形按钮。input:checked + .slider
选择器用于在复选框被选中时改变滑块的颜色和圆形按钮的位置。.slider.round
类确保滑块和圆形按钮都是圆角的。上一篇:html 圆角
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站