当前位置:Gxlcms > css > css3实现switch组件开关

css3实现switch组件开关

时间:2021-07-01 10:21:17 帮助过:25人阅读

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

基于表单的checkbox

效果图

原理

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

html代码


  1. <input class='switch-component' type='checkbox'>

css代码


  1. // switch组件
  2. .switch-component {
  3. position: relative;
  4. width: 60px;
  5. height: 30px;
  6. background-color: #dadada;
  7. border-radius: 30px;
  8. border: none;
  9. outline: none;
  10. -webkit-appearance: none;
  11. transition: all .2s ease;
  12. }
  13. // 按钮
  14. switch-component::after {
  15. content: '';
  16. position: absolute;
  17. top: 0;
  18. left: 0;
  19. width: 50%;
  20. height: 100%;
  21. background-color: #fff;
  22. border-radius: 50%;
  23. transition: all .2s ease;
  24. }
  25. // checked状态时,背景颜色改变
  26. .switch-component:checked {
  27. background-color: #86c0fa;
  28. }
  29. // checked状态时,按钮位置改变
  30. .switch-component:checked::after {
  31. left: 50%;
  32. }

相关推荐:

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

以上就是css3实现switch组件开关的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行