简体   繁体   English

汉堡菜单图标类切换

[英]Hamburger Menu Icon class toggling

I have implemented a hamburger icon using font awesome icons fa-bars and fa-close icons.我已经使用字体很棒的图标 fa-bars 和 fa-close 图标实现了一个汉堡包图标。

<div class="btn-group box" id="homeNavMenuBox" onclick="toggleMenu(this)">
    <button type="button" class="homenavmenu-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
        <span class="fa fa-bars fa-2x"></span>
    </button>
</div>
<div aria-expanded="true" class="navbar-collapse navbar-responsive-collapse collapse in">
    <ul>
        <li> About </li>
        <li> Info</li>
        <li> Logout</li>
     </ul>
 </div>

Now, On click of the div, toggling the class to close icon.现在,单击 div,切换类以关闭图标。 Clicking on Close icon will bring bars icon back thus giving the feeling of menu and close icons.单击关闭图标将带回条形图标,从而给人一种菜单和关闭图标的感觉。

function toggleMenu(menuClass) {
    jQuery(menuClass).find('span').toggleClass('fa fa-bars fa fa-close');
}

It works fine with normal clicks, however, when user rapidly clicks on icons, their roles are getting reversed, ie when menu gets expanded instead of close icon, I am getting menu icon and vice versa它适用于普通点击,但是,当用户快速点击图标时,他们的角色会颠倒过来,即当菜单被展开而不是关闭图标时,我得到菜单图标,反之亦然

Expected and Actual functionalities of Menu Icon菜单图标的预期和实际功能

Instead of setting the icon CSS classes on toggle, attach the menu to the "shown" and "hidden" custom collapse events exposed by the Bootstrap API .不要在切换时设置图标 CSS 类,而是将菜单附加到Bootstrap API公开的“显示”和“隐藏”自定义折叠事件。 This will help ensure the menu is completely opened or completely closed prior changing the icons, which should help with avoiding the wrong icons appearing at the wrong time:这将有助于确保在更改图标之前完全打开或完全关闭菜单,这应该有助于避免在错误的时间出现错误的图标:

HTML: HTML:

<div id="someIdentifier" aria-expanded="true" class="navbar-collapse navbar-responsive-collapse collapse in">

jQuery: jQuery:

$(document).ready(function(){
    var $menu = $('#homeNavMenuBox');
    var $toggle = $menu.find('span');

    // query collapsing element to attach event handlers to
    var $collapse = $('#someIdentifier');

    $collapse.on('shown.bs.collapse', function() {
        toggleMenu();
    });

    $collapse.on('hidden.bs.collapse', function() {
        toggleMenu();
    });

    function toggleMenu() {
        $toggle.toggleClass("fa-bars fa-close");
    }
});

Also this demonstrating storing the jQuery query rather for the span rather than executing a new jQuery query each toggle as both #homeNavMenuBox and the span will likely be present the entire time.此外,这演示了存储 jQuery 查询而不是span而不是在每次切换时执行新的 jQuery 查询,因为#homeNavMenuBoxspan可能会一直存在。

Hopefully that helps!希望这有帮助!

An other option would be to use the second argument for the toggleClass method, that determines the state.另一种选择是使用 toggleClass 方法的第二个参数,该方法确定状态。

function toggleMenu(menuClass) {
var el = jQuery(menuClass).find('span');
el.toggleClass('fa-bars fa-close', function(){
    return el.hasClass('fa-bars');
});

} }

The class will now be toggled depending on whether the menu is open or not.现在将根据菜单是否打开来切换类。

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

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