CSS3 Switch

在这里插入图片描述

除了下面这种方案实现switch,还可以使用 before + after 配合上 opacity 透明度的 0 和 1,来实现按钮的切换。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf8"/>
<style>
:root {
	--switch-width: 3em;
	--switch-height: calc(var(--switch-width) / 2);
	--switch-anim-duration: 0.5s;
	font-size: 100%;
}
.switch {
	display: none;
}
.switch-label {
	width: var(--switch-width);
	height: var(--switch-height);
	border: 1px solid #AAA;
	border-radius: calc(var(--switch-width) / 4);
	background-color: #EEE;
	cursor: pointer;
	display: inline-block;
	transition: opacity var(--switch-anim-duration) ease;
	position: relative;
}
.switch-label::before {
	content: '';
	display: inline-block;
	width: 50%;
	height: 100%;
  	background-color: white;
	border-radius: 100%;
	box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.08);
	transition: all var(--switch-anim-duration) ease;
	position: absolute;
}
/*
相临兄弟选择器(单选)	p+ul
兄长选择器(多选)		p~ul
子代选择器(多选)		p>ul
后代选择器(多选)		p ul
*/
.switch:checked~label:before {
	transform: translateX(calc(var(--switch-width) / 2));
}
#switch:checked~label {
	background-color: green;
}
</style>
<script>
function onStateChange() {
	console.log(document.getElementById("switch").checked);
}
</script>
</head>
<body>
	<input type="checkbox" id="switch" class="switch" />
	<label for="switch" class="switch-label" onclick="onStateChange()"></label>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值