首页 > web前端 > html教程 > css3实现switch开关效果

css3实现switch开关效果

WBOY
发布: 2016-08-31 08:41:47
原创
1452 人浏览过

之前阿里电面的时候问的一个问题,今天抽时间做了个demo。

html结构

    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="bg_con"</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="checked_1"</span><span style="color: #ff0000;"> type</span><span style="color: #0000ff;">="checkbox"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="switch"</span> <span style="color: #0000ff;">/></span>
            <span style="color: #0000ff;"><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="checked_1"</span><span style="color: #0000ff;">></span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span>
登录后复制

css代码,:before负责颜色,:after是那个白色小圆点,切换时的过渡效果用css3的动画实现。 

<span style="color: #800000;">.switch</span>{<span style="color: #ff0000;">
        display</span>:<span style="color: #0000ff;">none</span>;
    }<span style="color: #800000;">
    label</span>{<span style="color: #ff0000;">
        position</span>:<span style="color: #0000ff;">relative</span>;<span style="color: #ff0000;">
        display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;">
        padding</span>:<span style="color: #0000ff;"> 1px</span>;<span style="color: #ff0000;">
        border-radius</span>:<span style="color: #0000ff;"> 24px</span>;<span style="color: #ff0000;">
        height</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;">
        margin-bottom</span>:<span style="color: #0000ff;"> 15px</span>;<span style="color: #ff0000;">
        background-color</span>:<span style="color: #0000ff;"> #eee</span>;<span style="color: #ff0000;">
        cursor</span>:<span style="color: #0000ff;"> pointer</span>;<span style="color: #ff0000;">
        vertical-align</span>:<span style="color: #0000ff;"> top</span>;<span style="color: #ff0000;">
        -webkit-user-select</span>:<span style="color: #0000ff;"> none</span>;
    }<span style="color: #800000;">
    label:before</span>{<span style="color: #ff0000;">
        content</span>:<span style="color: #0000ff;"> ''</span>;<span style="color: #ff0000;">
        display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;">
        border-radius</span>:<span style="color: #0000ff;"> 24px</span>;<span style="color: #ff0000;">
        height</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;">
        background-color</span>:<span style="color: #0000ff;"> white</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> scale(1, 1)</span>;<span style="color: #ff0000;">
        -webkit-transition</span>:<span style="color: #0000ff;"> all 0.3s ease</span>;
    }<span style="color: #800000;">
    label:after</span>{<span style="color: #ff0000;">
        content</span>:<span style="color: #0000ff;"> ''</span>;<span style="color: #ff0000;">
        position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;">
        top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">  
        left</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">  
        margin-top</span>:<span style="color: #0000ff;"> -11px</span>;<span style="color: #ff0000;">  
        margin-left</span>:<span style="color: #0000ff;"> -11px</span>;<span style="color: #ff0000;">
        width</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;">
        height</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;">
        border-radius</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;">
        background-color</span>:<span style="color: #0000ff;"> white</span>;<span style="color: #ff0000;">
        box-shadow</span>:<span style="color: #0000ff;"> 1px 1px 1px 1px rgba(0,0,0,0.08)</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translateX(-9px)</span>;<span style="color: #ff0000;">
        -webkit-transition</span>:<span style="color: #0000ff;"> all 0.3s ease</span>;
    }<span style="color: #800000;">
    .switch:checked~label:after</span>{<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translateX(9px)</span>;
    }<span style="color: #800000;">
    
    .switch:checked~label:before</span>{<span style="color: #ff0000;">
        background-color</span>:<span style="color: #0000ff;">green</span>;
    }  
登录后复制

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板