<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/2.2.4/jquery.min.js" type="application/javascript"></script>
<div class="switch">
<input type="checkbox" id="switch__checkbox" />
<label for="switch__checkbox" class="switch__label">
<span class="switch__circle">
</span>
</label>
</div>
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
SCSS
格式化
*,*:before,*:after{
box-sizing: border-box;
margin: 0;
padding: 0;
}
body{
background: #eff3e4;
}
@mixin switchOn(){
#switch__checkbox:checked ~ .switch__label & {
@content
}
}
$switch-bg:#0b1821;
$border:7px;
$border-rad:45px;
$border-color:#c3b912;
$w:190px;
$h:80px;
$animTime:1s;
//circle
$circle-h:49px;
$circle-w:49px;
.switch-bg{
background: $switch-bg;
}
.switch-border{
border:#0a2c42 solid 7px !important;
}
body{
transition:$animTime;
z-index:10;
}
#switch__checkbox{
z-index:-10;
position:absolute;
left:0;
top:0;
display:none;
}
.switch{
z-index:99;
position:absolute;
overflow:hidden;
width: $w;
height: $h;
left:50%;
top:50%;
border:$border solid $border-color;
border-radius:$border-rad;
margin-left: $w/-2;
margin-top: $h/-2;
&__label{
overflow:hidden;
z-index:10;
position:absolute;
width: 100%;
height: 100%;
}
&__circle{
position:absolute;
z-index:10;
width: $circle-w;
height: $circle-h;
background: #d9d726;
border-radius:50%;
top:0;
left:0;
margin-left: 10px;
margin-top: 9px;
transition:all $animTime;
//transform-origin:90%,70%;
#switch__checkbox:not(checked) ~.switch__label &{
animation:size-back $animTime/1.6 linear;
-moz-animation:size-back $animTime/1.6 linear both;
}
@include switchOn{
transform:translateX(107px);
-moz-transform:translateX(107px);
background: #fff;
animation:size $animTime/1.6 linear;
-moz-animation:size $animTime/1.6 linear;
}
&:before{
position:absolute;
z-index:-10;
content:'';
width: $circle-w;
height: $circle-h;
top:0;
left:-50px;
background: #eff3e4;
border-radius:50%;
transition:all $animTime;
-moz-transition:all $animTime;
@include switchOn{
transform:translate3d(65%,0,0);
-moz-transform:translate3d(65%,0,0);
}
@include switchOn{
background: $switch-bg;
}
}
}
}
//animation forward
@keyframes size {
0%{
transform:translateX(0) scale(1);
background:#d9d726;
}
55% {
transform:translateX(45px) scale(0.5);
background:#d9d726;
}
100%{
transform:translateX(108px) scale(1);
}
}
//animation back
@keyframes size-back {
0%{
transform:translateX(108px) scale(1);
background:white;
}
55% {
transform:translateX(45px) scale(0.5);
}
100%{
transform:translateX(0) scale(1);
}
}
JS
格式化
$(document).ready(function() {
$('.switch__label').click(function() {
$('body').toggleClass('switch-bg');
$('.switch').toggleClass('switch-border');
});
});