写一个原生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>
相关文章
|
10天前
|
图形学 UED
unity Tab键实现切换输入框功能
该脚本用于简化输入框之间的Tab键切换操作。只需将脚本挂载在InputField上,无需其他设置。脚本通过监听Tab键和Shift键组合,自动选择下一个或上一个可交互的InputField,提升用户体验。
|
9月前
|
JavaScript 前端开发
Bootstrap-Switch开关控件使用指南
Bootstrap-Switch开关控件使用指南
|
4月前
|
数据可视化
鸿蒙ArkUI实现开关switch组件
鸿蒙ArkUI实现开关switch组件
83 0
|
6月前
|
XML 数据格式
Andriod中为Dialog设置动画
Andriod中为Dialog设置动画
73 1
|
9月前
|
移动开发 JavaScript 小程序
uView Switch 开关选择器
uView Switch 开关选择器
70 1
|
iOS开发
自定义Switch控件
做iOS开发也有一段时间的了,项目中的开关控件一直都是用的系统级别的,至多就是给UISwitch控件换一个tintColor。这次的UI设计师设计了一个带有动画效果的UISwitch控件。
自定义Switch控件
|
9月前
|
移动开发 JavaScript 小程序
uniapp中组件库丰富的Switch 开关选择器使用方法
uniapp中组件库丰富的Switch 开关选择器使用方法
534 1
|
小程序
微信小程序:switch开关选择器样式
微信小程序:switch开关选择器样式
810 0
uniapp switch按钮的使用开关按钮效果demo(整理)
uniapp switch按钮的使用开关按钮效果demo(整理)
|
JavaScript 开发者
组件切换-应用切换动画和 mode 方式|学习笔记
快速学习组件切换-应用切换动画和 mode 方式
139 0
组件切换-应用切换动画和 mode 方式|学习笔记

热门文章

最新文章