2核1G3M服务器88一季度

腾讯云,阿里云百度云等 折扣价→点我←

快速掌握—HTML快速实现自定义Input开关 个人分享

仙阳网络 四级用户组

HTML


<input id="customSwitch" type="checkbox" />

<label for="customSwitch" class="switch"></label>

CSS


/* 定义全局变量 */

:root {

  --base_color: rgba(0, 0, 0, 0.25);

  --act_color: #5dcb61;

}


/* 隐藏input输入框 */

#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);

}

核心知识点


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


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


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

站长窝论坛版权声明 1、本帖标题:快速掌握—HTML快速实现自定义Input开关
2、论坛网址:站长窝论坛
3、站长窝论坛的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
4、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
5、站长窝论坛一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本帖由仙阳网络在站长窝论坛《交流综合区》版块原创发布, 转载请注明出处!
评论
最新回复 (1)
返回
发新帖