HTML
<input id="customSwitch" type="checkbox" />
<label for="customSwitch" class="switch"></label>
CSS
/* 定义全局变量 */
:root {
--base_color: rgba(0, 0, 0, 0.25);
--act_color: #5dcb61;
}
/* 隐藏input输入框 */
#customSwitch {
position: absolute;
left: -9999px;
}
/* 设置自定义颜色 */
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
background-color: var(--base_color);
border-radius: 20px;
transition: all 0.3s 0s;
}
/* 开关圆球 */
.switch::after {
content: "";
position: absolute;
top: 1px;
left: 1px;
width: 18px;
height: 18px;
border-radius: 18px;
background-color: white;
transition: all 0.3s 0s;
}
input[type="checkbox"]:checked + .switch::after {
transform: translateX(20px);
}
input[type="checkbox"]:checked + .switch {
background-color: var(--act_color);
}
核心知识点
隐藏真实input输入框,通过label for属性与input输入框绑定。
label标签本身作为椭圆形背景,用伪类作为开关圆球。
input选中后,需要单独设置label标签本体和伪类的移动
相关文章
- 基于HTML5 WebGL的工业化3D电子围栏
- 基于 HTML5 WebGL 和 VR 技术的 3D 机房数据中心可视
- HTML横向滚动条和文本超出显示三个小圆点
- 快速开发 HTML5 交互式地铁线路图
- 基于 HTML5 WebGL 与 WebVR 3D 虚实现实的可视化培训
- C# 将PDF转为Word、Html、XPS、SVG、PCL、PS——基于S
- 基于 HTML5 WebGL 的 3D 科幻风机
- 基于 HTML5 WebGL 的 3D 服务器与客户端的通信
- 基于 HTML5 + WebGL 的无人机 3D 可视化系统
- Angular 利用 marked.js 添加 Markdown + HTML 同时渲
- 手动滑稽之golang-vmware-driver广告篇