前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >label+input实现开关切换效果

label+input实现开关切换效果

作者头像
用户1749219
发布2018-05-16 11:42:10
6520
发布2018-05-16 11:42:10
举报

Document

<!-- .ipt { display: none; } .box { width: 74px; height: 30px; line-height: 30px; overflow: hidden; border: 1px solid #eee; border-radius: 4px; position: relative; cursor: pointer; } label {display:inline-block;} .ipt:checked + .box .switch-btn { left: 0; } .switch-btn { position: absolute; left: -37px; top: 0; width: 111px; height: 30px; transition: all 0.5s; } .switch-btn span{ width: 37px; height: 30px; display: block; text-align: center; float: left; font-size: 14px; } .on { background: #52B13C; color: white; } .white { background: white; } .off { background: #EEEEEE; } -->

主要使用label+input来实现改变left的值,下面是核心代码,意思就是选中的input的兄弟节点.box下的.switch-btn元素的left会变成0px(原来是-37px);

        .ipt:checked + .box .switch-btn {
            left: 0;
        }

当然要配合transition来实现

下面是效果

ON OFF

全部css代码

            .ipt {
                display: none;
            }
            .box {
                width: 74px;
                height: 30px;
                line-height: 30px;
                overflow: hidden;
                border: 1px solid #eee;
                border-radius: 4px;
                position: relative;
                cursor: pointer;
            }
            .ipt:checked + .box .switch-btn {
                left: 0;
            }
            .switch-btn {
                position: absolute;
                left: -37px;
                top: 0;
                width: 111px;
                height: 30px;
                transition: all 0.5s;
            }
            .switch-btn span{
                width: 37px;
                height: 30px;
                display: block;
                text-align: center;            
                float: left;
                font-size: 14px;
            }
            .on {
                background: #52B13C;
                color: white;
            }
            .white {
                background: white;
            }
            .off {
                background: #EEEEEE;
            }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档