html5+css3胶囊按钮代码

效果

代码

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<title></title>  
<style>  
    /* 胶囊开关的样式 */  
    .switch {  
        position: relative;  
        display: inline-block;  
        width: 60px;  
        height: 34px;  
    }  
    /*隐藏input*/
    .switch input {  
        display: none;  
    }
    /*胶囊外形*/
    .slider {  
        position: absolute;  
        cursor: pointer;  
        top: 0;  
        left: 0;  
        right: 0;  
        bottom: 0;  
        background-color: #ccc;  
        -webkit-transition: .4s;  
        transition: .4s;
        border-radius: 26px;
    }  
    /*胶囊里面的圆圈*/
    .slider::before {  
        position: absolute;  
        content: "";  
        height: 26px;  
        width: 26px;  
        left: 4px;  
        bottom: 4px;  
        background-color: white;  
        -webkit-transition: .4s;  
        transition: .4s;
        border-radius: 50%;
    }

    /*当input被点击时*/
    /*改变颜色*/
    input:checked + .slider {  
        background-color: #2196F3;  
    }  
   
    /*移动位置*/
    input:checked + .slider::before {  
        -webkit-transform: translateX(26px);  
        -ms-transform: translateX(26px);  
        transform: translateX(26px);  
    }  
  
    
</style>  
</head>  
<body>  
  
<label class="switch">  
    <input type="checkbox">  
    <span class="slider round"></span>  
</label>  
  
<script>  
    // JavaScript用于处理开关状态(如果需要)  
    var switchCheckbox = document.querySelector('.switch input[type="checkbox"]');  
    var slider = document.querySelector('.slider');  
  
    switchCheckbox.addEventListener('change', function() {  
        slider.classList.toggle('round');  
    });  
</script>  
  
</body>  
</html>
相关推荐
晚星star9 分钟前
在 Web 前端实现流式 TTS 播放
前端·vue.js
huabuyu9 分钟前
基于 Taro 的 Markdown AST 渲染器实现
前端
薄雾晚晴10 分钟前
Rspack 性能优化实战:JS/CSS 压缩 + 代码分割,让产物体积直降 40%
前端·javascript
本末倒置18310 分钟前
前端面试高频题:18个经典技术难点深度解析与解决方案
前端·vue.js·面试
狗头大军之江苏分军31 分钟前
Meta万人裁员亲历者自述:小扎尝到了降本的甜头
前端·后端·github
秃顶老男孩.36 分钟前
web中的循环遍历
开发语言·前端·javascript
龙在天1 小时前
vue 请求接口快慢 覆盖 解决方案
前端
跟橙姐学代码1 小时前
Python 类的正确打开方式:从新手到进阶的第一步
前端·python·ipython
Jagger_1 小时前
SonarQube:提升代码质量的前后端解决方案
前端·后端·ai编程
Becauseofyou1371 小时前
如果你刚入门Three.js,这几个开源项目值得你去学习
前端·three.js