Bootstrap mobile menu icon change to x close

后端 未结 8 1542
一向
一向 2021-02-05 13:54

EDIT

@zim answer uses 2020 CSS to easily solve the issue and better applies to Bootstrap 4.

The original question and selected answer are still

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 14:37

    Your JavaScript replaces the inner html of the #ChangeToggle element to show either the X or the hamburger icon. Precisely clicking on the X or the hamburger menu instead of the #ChangeToggle will remove the element being clicked on which I think prevents it from bubbling up. As Bootstrap's collapse plugin uses an event handler on the document to determine if an element has been clicked, the collapse plugin will never get notified.

    I've created a small example where a click handler on the pink .outer area replaces the green .inner area. Note that clicking on the pink area (your #ChangeToggle) will lead to two events, where clicking on the green area (your X icon) will lead to a single event.

    $(function() {
      $('.outer')
        .click(function() {
          $('.outer').html('
    '); fired('.js-outer'); }); $(document).on('click', '.outer', function() { fired('.js-document'); }); }); function fired(el) { $(el).addClass('event--fire'); window.setTimeout(function() { $(el).removeClass('event--fire') }, 100); }
    body {
      font-family: Helvetica Neue, Helvetica, Arial;
    }
    .outer,
    .inner {
      display: inline-block;
      padding: 20px;
    }
    .outer {
      border: 1px solid #c66;
      background-color: #f99;
    }
    .inner {
      border: 1px solid #6c6;
      background-color: #9f9;
    }
    .event {
      margin: 10px 0;
    }
    .event::before {
      display: inline-block;
      content: '';
      border: 1px solid #ccc;
      padding: 10px;
      vertical-align: middle;
      margin-right: 10px;
    }
    .event--fire:before {
      background-color: #ff9;
    }
    
    
    
    
    Event fires on .outer
    Event fires on document

    The easiest way to solve this issue for your navigation bar is to hide/show the X or hamburger icon instead of replacing. In the example below both the X and the hamburger icon are in the html, and toggling the class .hidden is used to show the correct icon.

    $(function() {
      $('#ChangeToggle').click(function() {
        $('#navbar-hamburger').toggleClass('hidden');
        $('#navbar-close').toggleClass('hidden');  
      });
    });
    
    
    
    
    

    Instead of adding a jQuery click handler next to Bootstrap's collapse plugin, you could also use the events fired by the collapse plugin to hide or show the correct icon. Use the shown.bs.collapse event to show the X icon, and the hidden.bs.collapse event to show the hamburger icon.

    $(function() {
      
      $('#bs-example-navbar-collapse-1')
        .on('shown.bs.collapse', function() {
          $('#navbar-hamburger').addClass('hidden');
          $('#navbar-close').removeClass('hidden');    
        })
        .on('hidden.bs.collapse', function() {
          $('#navbar-hamburger').removeClass('hidden');
          $('#navbar-close').addClass('hidden');        
        });
      
    });
    #navbar-close {
      color: #888;
      width: 22px;
      height: 14px;
    }
    
    
    
    
    

提交回复
热议问题