如何使用CSS创建“拨动开关”(打开/关闭按钮)?
要使用CSS创建切换开关,代码如下-
示例
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
span {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
span:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + span {
background-color: rgb(85, 21, 138);
}
input:focus + span {
box-shadow: 0 0 1px rgb(255, 77, 211);
}
input:checked + span:before {
transform: translateX(26px);
}
span.round {
border-radius: 34px;
}
span.round:before {
border-radius: 50%;
}
</style>
</head>
<body>
<h1>Toggle Switch Example</h1>
<label class="switch">
<input type="checkbox">
<span class="round"></span>
</label>
<label class="switch">
<input type="checkbox" checked>
<span class="round"></span>
</label>
</body>
</html>输出结果
上面的代码将产生以下输出-
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短