PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

css3实现switch组件开关

小云云
小云云 原创
2018-02-11 10:27:36 2693浏览

本文主要和大家介绍了如何用css3实现switch组件的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。

基于表单的checkbox

效果图

原理

checkbox, 有两种状态, 没选中和选中, 当选中的时候(:checked), 改变样式即可, 首先得清除浏览器默认的样式, apperance: none, 本文代码只在chrome中运行, 如果需要写兼容性代码, 给apperance和transition加上前缀就好

html代码


<input class=&#39;switch-component&#39; type=&#39;checkbox&#39;>

css代码


// switch组件
.switch-component {
  position: relative;
  width: 60px;
  height: 30px;
  background-color: #dadada;
  border-radius: 30px;
  border: none;
  outline: none;
  -webkit-appearance: none;
  transition: all .2s ease;
}

// 按钮
switch-component::after {
  content: &#39;&#39;;
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background-color: #fff;
  border-radius: 50%;
  transition: all .2s ease;
 }

// checked状态时,背景颜色改变
.switch-component:checked {
  background-color: #86c0fa;
}

// checked状态时,按钮位置改变
.switch-component:checked::after {
  left: 50%;
 }

相关推荐:

微信小程序switch组件详细介绍

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。