<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<style>
.text-switch {
display: none;
}
.text-switch + label {
display: inline-block;
box-sizing: border-box;
height: 22px;
min-width: 44px;
line-height: 20px;
vertical-align: middle;
border-radius: 100px;
border: 1px solid transparent;
background-color: rgba(0, 0, 0, 0.25);
cursor: pointer;
-webkit-transition: all 0.36s;
transition: all 0.36s;
position: relative;
}
.text-switch:checked + label {
background-color: #1890ff;
}
.text-switch + label::before {
content: "";
display: block;
width: 18px;
height: 18px;
position: absolute;
left: 1px;
top: 1px;
border-radius: 18px;
background-color: #fff;
cursor: pointer;
transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-box-shadow: 0 2px 4px 0 rgba(0, 35, 11, 0.2);
box-shadow: 0 2px 4px 0 rgba(0, 35, 11, 0.2);
}
.text-switch:checked + label::before {
left: 23px;
transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
}
.text-switch + label::after {
content: "关";
position: absolute;
top: 1px;
left: 24px;
font-size: 12px;
color: #fff;
}
.text-switch:checked + label::after {
content: "开";
left: 5px;
}
</style>
<body>
<div>
<input type="checkbox" id="text-switch" class="text-switch" />
<label for="text-switch"></label>
</div>
<script>
// 获取当前开关
$("#text-switch").on("change", function () {
if ($(this).is(":checked")) {
switchStatus = $(this).is(":checked");
alert(switchStatus); // To verify
} else {
switchStatus = $(this).is(":checked");
alert(switchStatus); // To verify
}
});
</script>
</body>
</html>
html实现switch开关
最新推荐文章于 2025-06-08 14:49:01 发布