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>
相关推荐
张雨zy17 分钟前
Vue 项目管理数据时,Cookie、Pinia 和 LocalStorage 三种常见的工具的选择
前端·javascript·vue.js
五月君_24 分钟前
Nuxt UI v4.3 发布:原生 AI 富文本编辑器来了,Vue 生态又添一员猛将!
前端·javascript·vue.js·人工智能·ui
!执行35 分钟前
遇到 Git 提示大文件无法上传确实让人头疼
前端·github
坚持学习前端日记1 小时前
个人网站从零到盈利的成长策略
前端·程序人生
CamilleZJ1 小时前
eslint+prettier
前端·eslint·工程化·prettier
web小白成长日记2 小时前
深入理解 React 中的 Props:组件通信的桥梁
前端·javascript·react.js
tjswk20082 小时前
在ios上动态插入元素的列表使用:last-child样式可能不能及时生效
前端
小小荧2 小时前
CSS 写 SQL 查询?后端慌了!
前端·sql
小高0072 小时前
🔥3 kB 换 120 ms 阻塞? Axios 还是 fetch?
前端·javascript·面试
千寻girling2 小时前
面试官 : “ Vue 选项式api 和 组合式api 什么区别? “
前端·vue.js·面试