管理系统菜单 折叠展开基本思路

74 阅读1分钟

利用CSS样式,添加移除class名,来处理CSS样式,即改变菜单宽度并对应添加相应的过渡效果。

下面代码就是通过切换class mini-bar,来改变nav的宽度,同时为菜单width添加过渡效果transition: width .3s;,及内容区添加transition: all .3s;

eg:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

    <style>
        html,
        body {
            padding: 0;
            margin: 0;
        }

        #content {
            background-color: antiquewhite;
            height: 100vh;
        }

        .nav {
            width: 200px;
            background-color: pink;
            height: 100%;
            position: fixed;
            transition: width .3s;
        }

        .left {
            height: 100%;
            background-color: yellow;
            margin-left: 200px;
            transition: all .3s;
        }


        .mini-bar .nav {
            width: 100px;
            background-color: pink;
            height: 100%;
            position: fixed;
            transition: width .3s;
        }

        .mini-bar .left {
            height: 100%;
            background-color: yellow;
            margin-left: 100px;
            transition: all .3s;
        }
    </style>
</head>

<body>
    <div id="content">
        <div class="nav"></div>
        <div class="left">
            <div style="height: 50px;background-color: aqua;">
                <button onclick="test1()">折叠/展开</button>
            </div>
        </div>
    </div>
</body>
<script>
    function test1() {
        $('body').toggleClass('mini-bar');
    }


</script>

</html>

image.png

image.png