时间:2021-01-02来源:www.pcxitongcheng.com作者:电脑系统城
1.1 html 代码解析
| 1 2 3 4 5 |
<div class="checkbox"> <div class="inner" id="inner"> <div class="toggle" id="toggle"></div> </div></div> |
最外层 checkbox, 是按钮的整体, inner 是ON下绿色框所占的区域, toggle 是能点击的 ON 和 OFF 区域.
1.2 css 代码解析设置 body 字体和背景色
| 1 2 3 4 5 6 |
body{ margin: 0; padding: 0; font-family: sans-serif; background: #dcdcdc;} |
设置按钮的背景色, 位置, 圆形边框, 上下边框颜色和厚度
| 1 2 3 4 5 6 7 8 9 10 11 12 |
.checkbox{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100px; height: 50px; border-radius: 25px; background: linear-gradient(0deg, #d8d8d8, #cccccc); border-top: 0.02em solid #ececec; border-bottom: 0.02em solid #ececec;} |
设置 绿色底色区域 的上下左右位置, 以此确定宽度和高度, 注意, 这里没设宽度和高度, 默认是 auto
设置背景, 圆形边框, 阴影
| 1 2 3 4 5 6 7 8 9 10 11 |
.checkbox .inner{ position: absolute; /* 因为没设宽和高, 所以可以这样 */ top: 10px; left: 10px; right: 10px; bottom: 10px; background: linear-gradient(0deg, #a5a5a5, #717171); border-radius: 20px; box-shadow: inset 0 0 15px rgba(0,0,0,.5);} |
设置 ON OFF 按钮大小, 位置, 颜色, 背景, 阴影, 顶部和底部边框样式, 设置按钮上的动画时间为 0.5s
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
.checkbox .inner .toggle{ position: absolute; top: -3px; left: -3px; width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(0deg, #ccc, #e4e4e4); box-shadow: 0 4px 6px rgba(0,0,0,.2); box-sizing: border-box; border-top: 0.04em solid #ececec; border-bottom: 0.01em solid #ececec; transition: 0.5s;} |
设置 OFF 的样式,, 由上下左右的定位确定 宽和高, 设置背景, 圆形边框, line-height 用来设置字体垂直居中
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
.checkbox .inner .toggle:before{ content: "OFF"; position: absolute; top: 4px; left: 4px; right: 4px; bottom: 4px; background: linear-gradient(0deg, #e4e4e4, #ccc); border-radius: 50%; text-align: center; font-size: 10px; line-height: 28px; color: #a9a9a9;} |
设置 点击后按钮的字体, ON, 之所有没有写其他属性, 是因为其他属性都继承 checkbox .inner .toggle:before
| 1 2 3 4 |
.checkbox .inner.active .toggle:before{ content: "ON"; color: #00d22d;} |
当按钮被点击, 滑块右移, 并且更改背景色, 更改时间为 0.5s
| 1 2 3 4 5 6 |
.checkbox .inner.active .toggle{ left: 47px;}.checkbox .inner.active{ background: linear-gradient(0deg, #00d22d, #158a00);} |
1.3 javascript 代码解析
| 1 2 3 4 5 6 7 8 9 10 11 |
<script> let inner = document.getElementById('inner'); let toggle = inner.children[0]; toggle.addEventListener('click', ()=>{ if(inner.classList.contains('active')){ inner.classList.remove('active'); }else { inner.classList.add('active'); } })</script> |


2.1 html 源码
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="2020_12_24.css"></head><body><div class="checkbox"> <div class="inner" id="inner"> <div class="toggle" id="toggle"></div> </div></div><script> let inner = document.getElementById('inner'); let toggle = inner.children[0]; toggle.addEventListener('click', ()=>{ if(inner.classList.contains('active')){ inner.classList.remove('active'); }else { inner.classList.add('active'); } })</script></body></html> |
2.2 css 源码
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
body{ margin: 0; padding: 0; font-family: sans-serif; background: #dcdcdc;}.checkbox{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100px; height: 50px; border-radius: 25px; background: linear-gradient(0deg, #d8d8d8, #cccccc); border-top: 0.02em solid #ececec; border-bottom: 0.02em solid #ececec;}.checkbox .inner{ position: absolute; /* 因为没设宽和高, 所以可以这样 */ top: 10px; left: 10px; right: 10px; bottom: 10px; background: linear-gradient(0deg, #a5a5a5, #717171); border-radius: 20px; box-shadow: inset 0 0 15px rgba(0,0,0,.5);}.checkbox .inner .toggle{ position: absolute; top: -3px; left: -3px; width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(0deg, #ccc, #e4e4e4); box-shadow: 0 4px 6px rgba(0,0,0,.2); box-sizing: border-box; border-top: 0.04em solid #ececec; border-bottom: 0.01em solid #ececec; transition: 0.5s;}.checkbox .inner .toggle:before{ content: "OFF"; position: absolute; top: 4px; left: 4px; right: 4px; bottom: 4px; background: linear-gradient(0deg, #e4e4e4, #ccc); border-radius: 50%; text-align: center; font-size: 10px; line-height: 28px; color: #a9a9a9;}.checkbox .inner.active .toggle:before{ content: "ON"; color: #00d22d;}.checkbox .inner.active .toggle{ left: 47px;}.checkbox .inner.active{ background: linear-gradient(0deg, #00d22d, #158a00);} |
到此这篇关于CSS仿苹果平滑开关按钮效果的文章就介绍到这了
2023-03-06
css3鼠标滑过实现动画线条边框2023-03-06
css scroll-snap控制滚动元素的实现2023-03-06
CSS实现多层嵌套列表自动编号的示例代码传统的灰色纯色边框你是不是觉得太难看了?你是否想设计一些精美的边框,例如渐变、圆角、彩色的边框?那你来对地方了,本文将介绍如何用纯CSS就能实现具有渐变和圆角的彩色边框...
2023-03-06
今天给大家带来一个如何实现圆角三角形的方案,这个方案虽然可以实现,但是也是借助拼凑等方式来实现的,假如想一个div来实现圆角三角形,还是比较困难的。之前文章讲了如何实现对话框,里面介绍了三角形的实现方式。今天讲讲...
2023-03-06