2020_12_24javascript
源码css
<div class="checkbox"> <div class="inner" id="inner"> <div class="toggle" id="toggle"></div> </div> </div>
最外层 checkbox, 是按钮的总体, inner 是ON下绿色框所占的区域, toggle 是能点击的 ON 和 OFF 区域.html
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; }
设置 绿色底色区域 的上下左右位置, 以此肯定宽度和高度, 注意, 这里没设宽度和高度, 默认是 autojava
设置背景, 圆形边框, 阴影字体
.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; }
line-height
用来设置字体垂直居中.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 .toggle:before
.checkbox .inner.active .toggle:before{ content: "ON"; color: #00d22d; }
.checkbox .inner.active .toggle{ left: 47px; } .checkbox .inner.active{ background: linear-gradient(0deg, #00d22d, #158a00); }
<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>
<!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>
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); }