• html代码:
1
2
3
4
<div class="switch onswitch">
<em>开启</em>
<i></i>
</div>
  • css代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
.switch {
position: relative;
display: inline-block;
height: 22px;
line-height: 22px;
min-width: 35px;
padding: 0 5px;
margin-top: 8px;
border: 1px solid #d2d2d2;
border-radius: 20px;
cursor: pointer;
background-color: #fff;
-webkit-transition: .1s linear;
transition: .1s linear;
}

.switch em {
position: relative;
top: 0;
width: 25px;
margin-left: 21px;
padding: 0 !important;
text-align: center !important;
color: #999 !important;
font-style: normal !important;
font-size: 12px;
user-select: none;
}

.switch i {
position: absolute;
left: 5px;
top: 3px;
width: 16px;
height: 16px;
border-radius: 20px;
background-color: #d2d2d2;
-webkit-transition: .1s linear;
transition: .1s linear;
}

.onswitch {
border-color: #5FB878;
background-color: #5FB878;
}

.onswitch em {
margin-left: 5px;
margin-right: 21px;
color: #fff !important;
}

.onswitch i {
left: 100%;
margin-left: -21px;
background-color: #fff;
}
  • js代码
1
2
3
4
5
6
7
8
9
10
11
12
$('.switch').on('click', function(){
let isCk = $(this).hasClass('onswitch');
if(isCk){
//关
$(this).removeClass('onswitch');
$(this).find('em').html('关闭');
} else {
//开
$(this).addClass('onswitch');
$(this).find('em').html('开启');
}
});