经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
CSS仿苹果平滑开关按钮效果_CSS教程_CSS
来源:jb51  时间:2020/12/28 9:29:55  对本文有异议

源码

1. 代码解析

1.1 html 代码解析

  1. <div class="checkbox">
  2. <div class="inner" id="inner">
  3. <div class="toggle" id="toggle"></div>
  4. </div>
  5. </div>

最外层 checkbox, 是按钮的整体, inner 是ON下绿色框所占的区域, toggle 是能点击的 ON 和 OFF 区域.

1.2 css 代码解析设置 body 字体和背景色

  1. body{
  2. margin: 0;
  3. padding: 0;
  4. font-family: sans-serif;
  5. background: #dcdcdc;
  6. }

设置按钮的背景色, 位置, 圆形边框, 上下边框颜色和厚度

  1. .checkbox{
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. transform: translate(-50%, -50%);
  6. width: 100px;
  7. height: 50px;
  8. border-radius: 25px;
  9. background: linear-gradient(0deg, #d8d8d8, #cccccc);
  10. border-top: 0.02em solid #ececec;
  11. border-bottom: 0.02em solid #ececec;
  12. }

设置 绿色底色区域 的上下左右位置, 以此确定宽度和高度, 注意, 这里没设宽度和高度, 默认是 auto

设置背景, 圆形边框, 阴影

  1. .checkbox .inner{
  2. position: absolute;
  3. /* 因为没设宽和高, 所以可以这样 */
  4. top: 10px;
  5. left: 10px;
  6. right: 10px;
  7. bottom: 10px;
  8. background: linear-gradient(0deg, #a5a5a5, #717171);
  9. border-radius: 20px;
  10. box-shadow: inset 0 0 15px rgba(0,0,0,.5);
  11. }

设置 ON OFF 按钮大小, 位置, 颜色, 背景, 阴影, 顶部和底部边框样式, 设置按钮上的动画时间为 0.5s

  1. .checkbox .inner .toggle{
  2. position: absolute;
  3. top: -3px;
  4. left: -3px;
  5. width: 36px;
  6. height: 36px;
  7. border-radius: 50%;
  8. background: linear-gradient(0deg, #ccc, #e4e4e4);
  9. box-shadow: 0 4px 6px rgba(0,0,0,.2);
  10. box-sizing: border-box;
  11. border-top: 0.04em solid #ececec;
  12. border-bottom: 0.01em solid #ececec;
  13. transition: 0.5s;
  14. }

设置 OFF 的样式,, 由上下左右的定位确定 宽和高, 设置背景, 圆形边框, line-height 用来设置字体垂直居中

  1. .checkbox .inner .toggle:before{
  2. content: "OFF";
  3. position: absolute;
  4. top: 4px;
  5. left: 4px;
  6. right: 4px;
  7. bottom: 4px;
  8. background: linear-gradient(0deg, #e4e4e4, #ccc);
  9. border-radius: 50%;
  10. text-align: center;
  11. font-size: 10px;
  12. line-height: 28px;
  13. color: #a9a9a9;
  14. }

设置 点击后按钮的字体, ON, 之所有没有写其他属性, 是因为其他属性都继承 checkbox .inner .toggle:before

  1. .checkbox .inner.active .toggle:before{
  2. content: "ON";
  3. color: #00d22d;
  4. }

当按钮被点击, 滑块右移, 并且更改背景色, 更改时间为 0.5s

  1. .checkbox .inner.active .toggle{
  2. left: 47px;
  3. }
  4. .checkbox .inner.active{
  5. background: linear-gradient(0deg, #00d22d, #158a00);
  6. }

1.3 javascript 代码解析

  1. <script>
  2. let inner = document.getElementById('inner');
  3. let toggle = inner.children[0];
  4. toggle.addEventListener('click', ()=>{
  5. if(inner.classList.contains('active')){
  6. inner.classList.remove('active');
  7. }else {
  8. inner.classList.add('active');
  9. }
  10. })
  11. </script>
  • 首先获取到 inner 和 toggle, 一个背景色框, 一个圆形按钮
  • 给按钮 toggle 注册点击事件, 点击 ON OFF 按钮才有效
  • 当 inner 处于 active, 也就是 inner 元素包含 active 类, 那就移除, 如果不包含, 那就添加, 反正做一个相反的操作
  • 当 inner 处于 active, toggle 会右移, inner 背景色会变换, 在 css中 设置
  • 大功告成, 过程如下图


 

2. 源码

2.1 html 源码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link rel="stylesheet" href="2020_12_24.css">
  7. </head>
  8. <body>
  9. <div class="checkbox">
  10. <div class="inner" id="inner">
  11. <div class="toggle" id="toggle"></div>
  12. </div>
  13. </div>
  14. <script>
  15. let inner = document.getElementById('inner');
  16. let toggle = inner.children[0];
  17. toggle.addEventListener('click', ()=>{
  18. if(inner.classList.contains('active')){
  19. inner.classList.remove('active');
  20. }else {
  21. inner.classList.add('active');
  22. }
  23. })
  24. </script>
  25. </body>
  26. </html>

2.2 css 源码

  1. body{
  2. margin: 0;
  3. padding: 0;
  4. font-family: sans-serif;
  5. background: #dcdcdc;
  6. }
  7. .checkbox{
  8. position: absolute;
  9. top: 50%;
  10. left: 50%;
  11. transform: translate(-50%, -50%);
  12. width: 100px;
  13. height: 50px;
  14. border-radius: 25px;
  15. background: linear-gradient(0deg, #d8d8d8, #cccccc);
  16. border-top: 0.02em solid #ececec;
  17. border-bottom: 0.02em solid #ececec;
  18. }
  19. .checkbox .inner{
  20. position: absolute;
  21. /* 因为没设宽和高, 所以可以这样 */
  22. top: 10px;
  23. left: 10px;
  24. right: 10px;
  25. bottom: 10px;
  26. background: linear-gradient(0deg, #a5a5a5, #717171);
  27. border-radius: 20px;
  28. box-shadow: inset 0 0 15px rgba(0,0,0,.5);
  29. }
  30. .checkbox .inner .toggle{
  31. position: absolute;
  32. top: -3px;
  33. left: -3px;
  34. width: 36px;
  35. height: 36px;
  36. border-radius: 50%;
  37. background: linear-gradient(0deg, #ccc, #e4e4e4);
  38. box-shadow: 0 4px 6px rgba(0,0,0,.2);
  39. box-sizing: border-box;
  40. border-top: 0.04em solid #ececec;
  41. border-bottom: 0.01em solid #ececec;
  42. transition: 0.5s;
  43. }
  44. .checkbox .inner .toggle:before{
  45. content: "OFF";
  46. position: absolute;
  47. top: 4px;
  48. left: 4px;
  49. right: 4px;
  50. bottom: 4px;
  51. background: linear-gradient(0deg, #e4e4e4, #ccc);
  52. border-radius: 50%;
  53. text-align: center;
  54. font-size: 10px;
  55. line-height: 28px;
  56. color: #a9a9a9;
  57. }
  58. .checkbox .inner.active .toggle:before{
  59. content: "ON";
  60. color: #00d22d;
  61. }
  62. .checkbox .inner.active .toggle{
  63. left: 47px;
  64. }
  65. .checkbox .inner.active{
  66. background: linear-gradient(0deg, #00d22d, #158a00);
  67. }

到此这篇关于CSS仿苹果平滑开关按钮效果的文章就介绍到这了,更多相关css平滑开关按钮内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号