效果如下:
结构则是把input放在最上面并透明化,下面放.button,其中要用到定位(其实用margin也可以)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.box{
width: 240px;
height: 80px;
background-color: #353535;
position: relative;
overflow: hidden;
margin: 8px auto;
}
input{
display: block;
width: 240px;
height: 80px;
opacity: 0;
position: absolute;
z-index: 1;
}
.button::before{
display: block;
content: "";
width: 80px;
height: 80px;
background-color: #5DB423;
position: absolute;
left: 0;
top: 0;
transition: all 0.5s;
border-radius: 50%;
}
.button{
width: 240px;
height: 80px;
background-color: #87CEEB;
transition: all 0.5s;
border-radius: 80px;
}
input:checked +.button::before{
left: 160px;
background-color: #ff0000;
}
input:checked +.button{
background-color: #ffff7f;
}
</style>
</head>
<body>
<div class="box">
<input type="checkbox" name="" id="" value="" />
<div class="button"></div>
</div>
</body>
</html>
过渡效果
transition: all 0 ease 0;
trasition
为元素添加过渡属性,有四个值,分别是范围,持续时间,速度,生效前置时间.上面为其默认值.
-
范围有all ,none , 属性名称列表(用逗号隔开)
-
持续时间为number类型,用时间符号s,min等结尾
-
速度默认为ease,意思是由慢变快再变慢.有其他属性如linear,速度一致
-
生效前置时间指的经过指定时间后触发,默认为0秒.用法同持续时间.
checked选择器:
a:checked{
background:#ffffff;
}
是核心效果,没有他和兄弟选择器做不出效果.
这里的a:checked
也可以为空:checked
,主要是触发条件,checked意思是匹配单选按钮radio
或者复选按钮checkbox
的选中状态.从而进行操作.通常连用兄弟选择器.
兄弟选择器
兄弟选择器分为相邻和后续两种
- 相邻兄弟选择器:
div + p{}
只能选择div标签之后的,同父元素内的,第一个p标签
- 通用兄弟选择器:
div ~ p{}
会选择div标签之后的,同父元素的,所有p标签
before选择器:
p:before{}
选中的是p标签的前面部分,注意:需要变成块级元素之后才能设置宽高.
边框圆角:
borde-radius:80px;