Bootstrap CSS Active Navigation

前端 未结 13 2350

On the Bootstrap website the subnav matches up with the sections and changes background color as you or scroll to the section. I wanted to create my own menu without all the

13条回答
  •  情话喂你
    2020-11-27 12:51

    This is getting pretty old and clustered with answers, but I took the top answer and made it better. The top answer only works if there's one on the page. This scopes it to removing active from sibling elements and adding it to the one you clicked.

    I also added the class nav-toggle so that it doesn't interfere with things that already have bootstrap js attached.

    $('.nav.nav-toggle li').click(function(e) {
      var $this = $(this);
      $this.siblings().removeClass('active');
      if (!$this.hasClass('active')) {
          $this.addClass('active');
      }
      e.preventDefault();
    });
    

提交回复
热议问题