css3自定义开关

效果如下:
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
结构则是把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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值