CSS做一个Switch开关

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

CSS做⼀个Switch开关
本⽂为博主原创,转载请注明出处。


Switch开关:
根据需求可知,Switch开关只有两种选择,true或false。

所以我们想到HTML的checkbox控件,⽤它来做。


<input id="switch" type="checkbox" class="switch"/>
但是在浏览器中,checkbox是有固定形状的(对勾),所以我们并不能直接修改checkbox的样式。

那我们该修改⼀个什么东西的样式变成开关呢?所以我们联想到 label 标签,为什么呢?因为label标签的for属性可以绑定表单控件,点击label标签,就相当于你点击了绑定的那个控件。

<label for="switch">test</label>
废话不多说,直接上完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<link rel="stylesheet" href="css/test.css"/>
</head>
<body>
<div class="switch-container">
<input id="switch" type="checkbox" class="switch"/>
<label for="switch" onclick="test()"></label>
</div>
<script>
var test = function(){
console.log(!document.getElementById('switch').checked ? "选中" : "未选中");
}
</script>
</body>
</html>
/*开关的⼤⼩*/
.switch-container {
height: 15px;
width: 30px;
}
/*设置checkbox不显⽰*/
.switch {
display: none;
}
/*设置label标签为椭圆状*/
label {
display: block;
background-color: #EEEEEE;
height: 100%;
width: 100%;
cursor: pointer;
border-radius: 25px;
}
/*在label标签内容之前添加如下样式,形成⼀个未选中状态*/
label:before {
content: '';
display: block;
border-radius: 25px;
height: 100%;
width: 15px;
background-color: white;
opacity: 1;
box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.08);
-webkit-transition: all 0.2s ease;
}
/*在label标签内容之后添加如下样式,形成⼀个选中状态*/
label:after {
position: relative;
top: -15px;
left: 15px;
content: '';
display: block;
border-radius: 25px;
height: 100%;
width: 15px;
background-color: white;
opacity: 0;
box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.08);
-webkit-transition: all 0.2s ease;
}
/* ~ 兄弟选择符。

p~ul :位于 p 元素之后的所有 ul 元素
*/
/*选中后,选中样式显⽰*/
#switch:checked~label:after {
opacity: 1;
}
/*选中后,未选中样式消失*/
#switch:checked~label:before {
opacity: 0;
}
/*选中后label的背景⾊改变*/
#switch:checked~label {
background-color: green;
}
如果你⽤了sass,可以设置变量,来改变你的开关的⼤⼩。

$switchHeight: 30px;
$switchWidth: 60px; /*改变⼤⼩只需要改变这个两个的值,后⾯会⽤到这两个值*/ .switch-container {
height: $switchHeight;
width: $switchWidth;
margin-bottom: 5px;
.switch {
display: none;
}
label {
display: block;
background-color: #EEEEEE;
height: 100%;
width: 100%;
cursor: pointer;
border-radius: 25px;
}
label:before {
content: '';
display: block;
border-radius: 25px;
height: 100%;
width: $switchHeight;
background-color: white;
opacity: 1;
box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.08);
-ms-transition: all 0.2s ease; /* IE 9 */
-moz-transition: all 0.2s ease; /* Firefox */
-webkit-transition: all 0.2s ease;/* Safari and Chrome */ -o-transition: all 0.2s ease; /* Opera */
}
label:after {
position: relative;
top: -$switchHeight;
left: $switchHeight;
content: '';
display: block;
border-radius: 25px;
height: 100%;
width: $switchHeight;
background-color: white;
opacity: 0;
box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.08);
-ms-transition: all 0.2s ease; /* IE 9 */
-moz-transition: all 0.2s ease; /* Firefox */
-webkit-transition: all 0.2s ease;/* Safari and Chrome */ -o-transition: all 0.2s ease; /* Opera */
}
#switch:checked~label:after {
opacity: 1;
}
#switch:checked~label:before {
opacity: 0;
}
#switch:checked~label {
background-color: green;
}
}。

相关文档
最新文档