Modifying Bootstrap Active Navigation
Bootstrap's website boasts a subnav that effortlessly matches sections and alters background colors based on user input. This article aims to guide you through the steps of creating a simplified menu without the background changes, focusing on addressing issues with activating classes upon scrolling or clicking.
CSS Customization:
The provided HTML code appears to be standard, and the CSS revisions made are as follows:
.menu { list-style:none; } .menu > li { float: left; } .menu > li > a { color: #555; float: none; padding: 10px 16px 11px; display: block; } .menu > li > a:hover { color: #F95700; } .menu a[aria-current="page"], .menu a[aria-current="page"]:hover { color:#F95700; }
To implement the active class switching functionality, JavaScript is required. The provided answer leverages jQuery to achieve the desired effect:
$('.navbar li').click(function(e) { $('.navbar li.active').removeClass('active'); var $this = $(this); if (!$this.hasClass('active')) { $this.addClass('active'); } e.preventDefault(); });
This code ensures that upon clicking a menu item, all active classes are removed and the current item gains the active status. This aligns with the behavior observed in Bootstrap's subnav.
To implement this functionality, ensure proper linking of jQuery, bootstrap.js, and bootstrap.css files.
The above is the detailed content of Here are a few title options, keeping in mind the question format and focusing on the article\'s core content: **Option 1 (Direct and clear):** * **How to Create a Simplified Active Navigation Menu. For more information, please follow other related articles on the PHP Chinese website!