CSS3滑動開關按鈕效果

antzone發表於2018-06-02

分享一段程式碼例項,它實現了左右滑動的開關按鈕效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
input[type="checkbox"]{
  display:none;
}
input + label{
  box-shadow:rgb(0,0,0) 0px 0px 0px 1px;
  width:40px;
  height:20px;
  display:inline-block;
  border-radius:20px;
  position:relative;
  background-color:white;
  overflow:hidden;
}
input + label:before{
  content:'';
  position:absolute;
  box-shadow:rgb(0,0,0) 0px 0px 0px 1px;
  left:0px;
  width:20px;
  height:20px;
  display:inline-block;
  border-radius:20px;
  background-color:white;
  z-index:20;
  transition:all 0.5s;
}
input + label:after{
  content:'';
  position:absolute;
  border-radius:20px;
  left:-20px;
  width:40px;
  height:20px;
  display:inline-block;
  background-color:rgb(108, 185, 255);
  transition:all 0.5s;
}
input:checked + label:before{
  left:20px;
}
input:checked + label:after{
  left:0px;
}
</style>
</head>
<body>
<input type="checkbox" name="sex" id="male" />
<label for="male"></label>
</body>
</html>

相關文章