写一个原生switch开关按钮

简介: 欢迎阅读本博文,本博文主要讲述【写一个原生switch开关按钮】,文字通俗易懂,如有不妥,还请多多指正。

微信截图_20220425155134.png


<!DOCTYPE html>
<html>
<head>
  <title>switch开关按钮</title>
  <style type="text/css">
#checked {
 width: 60px;
 position: relative;
 margin: 20px auto;
}
.labelBox {
 margin-bottom: 20px;
 background: #409eff; 
 border-radius: 40px;
 width: 60px;
 position: relative;
 height: 32px;
}
.check { 
 display: block;
 width: 30px;
 height: 30px;
 border-radius: 50%;
 background: #fff ;
 border: 1px solid #e5e5e5;
 position: absolute;
 top: 0px;
 left: 0px;
}
input[type=checkbox] {
 border: 0 none !important;
 clip: rect(1px,1px,1px,1px);
 height: 1px !important;
 overflow: hidden !important;
 position: absolute !important;
 width: 1px !important;
}
@keyframes labelON {
 0% {
  top: 0px;
  left: 0px;
 }  
 100% { 
  top: 0px;
  left: 28px;
 }
}
@keyframes labelOFF {
 0% {
  top: 0px;
  left: 28px;
 }
 100% { 
  top: 0px;
  left: 0px;
 }
}
input[type=checkbox]:checked + label.check {
 top: 0px;
 left: 28px;  
 -webkit-animation: labelON 0.2s ease-in 0s 1;
 -moz-animation: labelON 0.2s ease-in 0s 1;
 -o-animation: labelON 0.2s ease-in 0s 1;
 -ms-animation: labelON 0.2s ease-in 0s 1;
 animation: labelON 0.2s ease-in 0s 1;
 box-shadow: #244766 -1px 0px 3px;
}
input[type=checkbox] + label.check {
 top: 0px;
 left: 0px;
 -webkit-animation: labelOFF 0.2s ease-in 0s 1;
 -moz-animation: labelOFF 0.2s ease-in 0s 1;
 -o-animation: labelOFF 0.2s ease-in 0s 1;
 -ms-animation: labelOFF 0.2s ease-in 0s 1;
 animation: labelOFF 0.2s ease-in 0s 1;
 box-shadow: #244766 1px 0px 3px;   
}
.status{
  text-align:center;
}
  </style>
</head>
<body>
<p class="status">1</p>
<div id="checked">
  <div class="labelBox">
    <input type="checkbox" value="wi-fi" id="wifi" name="wifi" checked="checked" />
    <label for="wifi" class="check"></label>
  </div>
</div>
</body>
<script type="text/javascript">
  var onOff=true;
  document.querySelector('.status').innerHTML=onOff;
  document.querySelector(".check").onclick=function (argument) {
    // body...
    onOff=!onOff;
    document.querySelector('.status').innerHTML=onOff
  }
</script>
</html>
相关文章
|
2月前
|
JavaScript 前端开发
Bootstrap-Switch开关控件使用指南
Bootstrap-Switch开关控件使用指南
|
27天前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
19 2
|
2月前
|
移动开发 JavaScript 小程序
uView Switch 开关选择器
uView Switch 开关选择器
27 1
|
12月前
|
iOS开发
自定义Switch控件
做iOS开发也有一段时间的了,项目中的开关控件一直都是用的系统级别的,至多就是给UISwitch控件换一个tintColor。这次的UI设计师设计了一个带有动画效果的UISwitch控件。
自定义Switch控件
|
2月前
|
移动开发 JavaScript 小程序
uniapp中组件库丰富的Switch 开关选择器使用方法
uniapp中组件库丰富的Switch 开关选择器使用方法
197 1
|
2月前
|
C++
QT 重写控件(QPushButton为例)实现背景图片的切换和鼠标样式切换
一般在QT开发中,使用setCursor()给控件设置鼠标的样式效果(一般是手型和箭头的切换),一般情况下,这个函数也是起作用的,但是一旦调用了全局QApplication::setOverrideCursor()设置鼠标效果后,在使用setCursor给控件设置鼠标样式就不起效果了,这是QT的机制
154 0
|
2月前
【鸿蒙软件开发】ArkTS常见组件之单选框Radio和切换按钮Toggle
【鸿蒙软件开发】ArkTS常见组件之单选框Radio和切换按钮Toggle
317 0
uniapp switch按钮的使用开关按钮效果demo(整理)
uniapp switch按钮的使用开关按钮效果demo(整理)
|
小程序
微信小程序:switch开关选择器样式
微信小程序:switch开关选择器样式
758 0
|
前端开发 JavaScript
JavaScript 监听移动端横竖屏状态的几种方式
JavaScript 监听移动端横竖屏状态的几种方式
685 0
JavaScript 监听移动端横竖屏状态的几种方式