码迷,mamicode.com
首页 > Web开发 > 详细

js实现点击一个按钮进行两种状态的切换(toggle)

时间:2016-12-25 01:09:23      阅读:4406      评论:0      收藏:0      [点我收藏+]

标签:button   lin   val   als   value   set   bsp   minimum   query   

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            width: 100%;
            overflow: hidden;
        }
        #leftMenu {
            /*position: absolute;*/
            height: 100%;
            width: 200px;
            background: rgba(0, 0, 0, 0.5);
            transition: all 0.5s;
            transform: translateX(-200px);
        }
        #leftMenu > ul {
            list-style: none;
        }
        #leftMenu > ul > li {
            margin-top: 5px;
            height: 50px;
            line-height: 50px;
            background-color: pink;
            color: #fff;
        }
        #main {
            width: 100%;
            height: 100%;
            background: blue;
            /*position: absolute;*/
            transition: all 0.5s;
        }

    </style>
</head>
<body>
    <section id="leftMenu">
        <ul>
            <li>首页</li>
            <li>首页#</li>
            <li>首页</li>
        </ul>
    </section>
    <main id="main">
        <input type="button" value="menu" id="btn">
    </main>
</body>
<script>
    var leftMenu = document.querySelector(‘#leftMenu‘);
    var btn = document.querySelector(‘#btn‘);
    var main = document.querySelector(‘#main‘);

    var falg = true;
    btn.addEventListener(‘click‘,function(){
        if(falg){
            leftMenu.style.transform = "translateX(0px)";
            main.style.transform = "translateX(200px)";
            falg=false;
        }else{
            leftMenu.style.transform = "translateX(-200px)";
            main.style.transform = "translateX(0px)";
            falg=true;
        }
    })
</script>
</html>

js实现点击一个按钮进行两种状态的切换(toggle)

标签:button   lin   val   als   value   set   bsp   minimum   query   

原文地址:http://www.cnblogs.com/songdongdong/p/6218683.html

(3)
(3)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!