简体   繁体   English

如何关闭侧边栏菜单? (引导程序)

[英]How can I close the sidebar menu? (Bootstrap)

I have a side-menu on my bootstrap webpage which is open by default. 我的引导网页上有一个侧面菜单,默认情况下是打开的。 If the screen is too small there is a button placed behind it to open the menu again. 如果屏幕太小,则屏幕后面会放置一个按钮以再次打开菜单。

When a user clicks on a link I would like the menu to close automatically. 当用户单击链接时,我希望菜单自动关闭。

The button I already have opens the menu perfectly, but I have no way of closing it? 我已经拥有的按钮可以完美地打开菜单,但是我无法关闭它吗?

<div id="wrapper">
        <div id="sidebar-wrapper">
            <ul class="sidebar-nav">
                <li class="sidebar-brand"><a href="#/"><img src="/content/img/AAA.png" width="27px" />Menu</a></li>
                <li data-ng-hide="!authentication.isAuth"><a href="#/Page2">Welcome {{authentication.userName}}</a></li>
                <li data-ng-hide="!authentication.isAuth"><a href="#/Page2">Page1</a></li>
                <li data-ng-hide="!authentication.isAuth"><a href="" data-ng-click="logOut()">Logout</a></li>
                <li data-ng-hide="authentication.isAuth"><a href="#/login"><span class="glyphicon glyphicon-user"></span> Login</a></li>
                <li data-ng-hide="authentication.isAuth"><a href="#/signup"><span class="glyphicon glyphicon-log-in"></span> Sign Up</a></li>
            </ul>
        </div>
    </div>

    <a href="#menu-toggle" class="btn btn-default" id="menu-toggle">
        Menu
    </a>

    <script>
        $("#menu-toggle").click(function (e) {
            e.preventDefault();
            $("#wrapper").toggleClass("toggled");
        });
    </script>

You could add in another event handler for the a elements. 您可以为a元素添加另一个事件处理程序。

    $(".sidebar-nav li a").click(function() {
        $("#wrapper").removeClass("toggled");
    });

Here are a few things that you could do: 您可以执行以下操作:

  • You could use hide/show with jQuery(I can see you are already using it). 您可以在jQuery中使用隐藏/显示(我可以看到您已经在使用它)。
  • Instead of makeing a "toggled" class, Bootstrap has a built in "hidden" class that you could use. Bootstrap没有创建“切换”类,而是提供了一个内置的“隐藏”类供您使用。
  • You could also toggle it's css "display" value. 您也可以切换它的CSS“显示”值。
  • You could use the "animate" class to slide the menu on and off the screen. 您可以使用“动画”类在屏幕上滑动菜单。
  • You could have a variable to store the state of the menu("true" for out, "false" for in), and change it when the toggle button is clicked. 您可能有一个变量来存储菜单的状态(“ true”表示输出,“ false”表示输入),并在单击切换按钮时对其进行更改。

I had a similar issue, and I placed a close button on the menu, and added a seperate click handler that would close the menu. 我遇到了类似的问题,我在菜单上放置了一个关闭按钮,并添加了一个单独的单击处理程序来关闭菜单。 The close button would then hide with the menu, leaving just the button to open the menu again. 然后,关闭按钮将与菜单一起隐藏,仅留下按钮以再次打开菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM