栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

快速掌握—HTML快速实现自定义Input开关

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速掌握—HTML快速实现自定义Input开关

HTML



CSS

:root {
  --base_color: rgba(0, 0, 0, 0.25);
  --act_color: #5dcb61;
}


#customSwitch {
	position: absolute;
	left: -9999px;
}


.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
  background-color: var(--base_color);
  border-radius: 20px;
  transition: all 0.3s 0s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 1px;
  left: 1px;
  width: 18px;
  height: 18px;
  border-radius: 18px;
  background-color: white;
  transition: all 0.3s 0s;
}

input[type="checkbox"]:checked + .switch::after {
  transform: translateX(20px);
}

input[type="checkbox"]:checked + .switch {
  background-color: var(--act_color);
}

核心知识点

  1. 隐藏真实input输入框,通过label for属性与input输入框绑定。

  2. label标签本身作为椭圆形背景,用伪类作为开关圆球。

  3. input选中后,需要单独设置label标签本体和伪类的移动

本系列旨在通过最直接的事例最完整的代码,解决一些开发中常遇到的实际问题。

例子链接:https://github.com/TianYiYang0225/high-ku/tree/master/customSwitch

喜欢的朋友可以点击关注一波~每天都会更新最实用的项目技巧

如果你觉得这篇文章帮助到了你,我很高兴。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/241894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号