swtich小开关

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
   * {
margin: 0;
padding: 0
}

.switch {
width: 50px;
margin-top: 3px;
height: 22px;
background: #4f7cfe;
cursor: pointer;
border-radius: 11px;
position: relative;
transition: all .1s linear;
}

.switch em {
position: absolute;
left: 5px;
top: 0;
line-height: 22px;
width: 25px;
color: #fff;
font-style: normal;
font-size: 12px;
transition: all .1s linear;
}

.switch i {
position: absolute;
left: 29px;
top: 1px;
background: #fff;
width: 20px;
height: 20px;
display: block;
border-radius: 10px;
transition: all .1s linear;
}

.switch.switchOn {
background: #ccc;
}

.switch.switchOn em {
left: 23px;
}

.switch.switchOn i {
left: 1px;
}

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="switch"><em>ON</em><i></i></div>
<script>
$(".switch").click(function() {
if ($(this).hasClass("switchOn")) {
$(this).removeClass("switchOn");
$(this).find("em").text("ON");
} else {
$(this).addClass("switchOn");
$(this).find("em").text("OFF");

}
})
</script>