CSS3侧栏滑出简单实现

使用css3 的 animation 属性实现的点击滑出侧栏

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
        <meta name="author" content="TKB-nice" />
        <style>
        *{
                margin: 0;
                padding: 0;
        }
        body{
                height: 100%;
                position: relative;
                top: 0;
                left: 0;
        }
        html{
                height: 100%;
                background-color: #eee;
        }
        li{
                list-style: none;
        }
        .page{
                height: 100%;
                width: 100%;
                overflow: hidden;
        }
        .list li:last-child{
                margin-top: 5px;
                background-color: skyblue;
        }

        .text{
                height: 50%;
        }
        .close{
                height: 50%;
        }
        .side-bar{
                display: none;
                width: 100%;
                height: 100%;
                background-color: orange;
                position: absolute;
                top: 0;
                z-index: 99;
                }

                @keyframes mymove
                {
                from {left:100%;}
                to {left:50%;}
                }
                @-webkit-keyframes mymove
                {
                from {left:100%;}
                to {left:0;}
                }
                @keyframes myleave
                {
                from {left:50%;}
                to {left:100%;}
                }
                @-webkit-keyframes myleave
                {
                from {left:0;}
                to {left:100%;}
                }
    </style>
    </head>
<body>
        <div class="page">
                <div class="mian">
                        <ul class="list">
                                <li class="item1" id="item1">侧栏1</li>
                                <li class="item2" id="item2">侧栏2</li>
                        </ul>
                </div>
                <div class="side-bar" id="side-bar1">
                        <p class="text">我是侧栏111111111</p>
                        <p class="close" id="close1">点击关闭</p>
                </div>
                <div class="side-bar" id="side-bar2">
                        <p class="text">我是侧栏2222222</p>
                        <p class="close" id="close2">点击关闭</p>
                </div>
        </div>

    <script>
    document.getElementById('item1').onclick=function(){
        document.getElementById('side-bar1').style.animation = 'mymove 0.5s ease forwards';
        document.getElementById('side-bar1').style.display = 'block';
    }
    document.getElementById('item2').onclick=function(){
        document.getElementById('side-bar2').style.animation = 'mymove 0.5s ease forwards';
        document.getElementById('side-bar2').style.display = 'block';
    }
    document.getElementById('close1').onclick=function(){
        document.getElementById('side-bar1').style.animation = 'myleave 0.5s ease forwards ';
        setTimeout(function(){
                document.getElementById('side-bar1').style.display = 'none';
        },500);

    }
    document.getElementById('close2').onclick=function(){
        document.getElementById('side-bar2').style.animation = 'myleave 0.5s ease forwards ';
        setTimeout(function(){
                document.getElementById('side-bar2').style.display = 'none';
        },500);

    }
    </script>
</body>
</html>

 

posted @ 2017-12-02 23:11  tang丶有年  阅读(2964)  评论(0编辑  收藏  举报