Trigger elements in the navigation bar as the user navigates through different sections of my single-page website

I've been trying to implement this particular example on my website, but I seem to be struggling with it.

Here are the lines that I added:

<script src="jQuery/jquery-2.1.1.min.js" type="text/javascript"> </script>
<script src="jQuery/navigation.js" type="text/javascript"> </script> <!--original script-->

I placed these lines between the header tags of my HTML file and made adjustments to my navigation bar as follows:

<nav class="floatingMenu" id="floatingMenu">
  <ul id="#top-menu">
    <li class="active"><a href="#">Top</a></li>
    <li><a class="button" href="#one">one</a></li>
    <li><a class="button" href="#two">two</a></li>
    <li><a class="button" href="#three">three</a></li>
    <li><a class="button" href="#four">four</a></li>
    <li><a class="button" href="#five">five</a></li>
    <li><a class="button" href="#six">six</a></li>
  </ul> 
</nav>

Furthermore, I have defined a CSS class specifying the style attributes for the menu elements.

.active {font-size:30px;}

Can someone identify where I may have gone wrong in this process?
Thank you!

Note: The following is the content of my jQuery/navigation.js:

// Cache selectors
var lastId,
topMenu = $("#floatingMenu"),
topMenuHeight = topMenu.outerHeight() + 15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function () {
    var item = $($(this).attr("href"));
    if (item.length) {
        return item;
    }
});

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function (e) {
var href = $(this).attr("href"),
    offsetTop = href === "#" ? 0 : $(href).offset().top - topMenuHeight + 1;
$('html, body').stop().animate({
    scrollTop: offsetTop
}, 300);
e.preventDefault();
});

// Bind to scroll
$(window).scroll(function () {
// Get container scroll position
var fromTop = $(this).scrollTop() + topMenuHeight;

// Get id of current scroll item
var cur = scrollItems.map(function () {
    if ($(this).offset().top < fromTop) return this;
});
// Get the id of the current element
cur = cur[cur.length - 1];
var id = cur && cur.length ? cur[0].id : "";

if (lastId !== id) {
    lastId = id;
    // Set/remove active class
    menuItems.parent().removeClass("active")
        .end().filter("[href=#" + id + "]").parent().addClass("active");
}

});

Answer №1

I found this in the JS you provided in the fiddle:

topMenu = $("#top-menu"),

Instead, use:

topMenu = $("#floatingMenu"),

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

verifying the user's screen dimensions

I'm currently exploring ways to customize the appearance of my website based on the viewer's screen resolution. I am interested in potentially optimizing the layout for vertical screens on mobile devices, and horizontal screens for laptops. Addit ...

What is the method for customizing the background color in a .vue file using Bootstrap?

Can anyone assist me in updating the background color of a div class from grey to white? The code is within a .vue file using Bootstrap and includes an enclosed SVG file. Here's the snippet: <div class="text-left my-3 bg-white"> <button var ...

Invoking a JQuery function when clicked

While this question may appear similar to others on this platform, I am facing a unique situation and struggling to make it work. It may be a simple solution that I am overlooking. Here is the basic dialog function that I want to trigger onclick with an "a ...

Code-behind not functioning properly for Bootstrap Modal

Whenever the password or username are incorrect, I need to open a modal and keep it in the 'Else' statement. However, it is not working, the modal does not open. protected void bntLogar_Click(object sender, EventArgs e) { Registrar c ...

What is the best method to activate or deactivate a link with jQuery?

Can you provide guidance on how to toggle the activation of an anchor element using jQuery? ...

Dynamically loading CSS files in an Angular 17 application during runtime

I have a specific need where numerous CSS files are stored on a public CDN server. My Angular 17 application requires the ability to extract a value from a query parameter and then use that value to locate the corresponding CSS file on the CDN server in o ...

Error encountered while using jQuery Ajax - unable to properly handle the error

Currently, I am having issues with my ajax query when trying to retrieve the login status. The function seems to be malfunctioning. function getdetails(playlistname) { alert(playlistname); $.ajax({ type: "POST", url: "model/login_details1.php", ...

The JQuery onchange event functions as expected multiple times within JSFiddle, but seems to only fire once in the

A jQuery (1.11.1) script has been implemented on a business catalyst site cart page to show or hide a message based on the dropdown option selected by the user. The script functions correctly multiple times in jsfiddle at http://jsfiddle.net/NathanHill/462 ...

The CSS3 animations using transit do not occur at the same time in Internet Explorer and Firefox

I am currently developing a presentation library to simplify my work. Naturally, animations play a significant role in this project. The library leverages Transit for smooth CSS3 animations. One specific animation involves an element sliding into or out o ...

Modify CSS class if the window size is smaller than specified value

Is there a way to modify the attributes of a CSS class if the browser window is less than 600px in height? The current default properties are as follows: .side { height:400px; width:700px; } I am looking to update it to: .side { height:300px; width:550p ...

How can I alter the background color using Jquery or CSS3?

UPDATE Update: After following Zach Saucier's instructions, I added the specified codes: JS this.onclick = function() { this.style.background = "red"; this.innerHTML = "Bought"; } So the button displays as: <a onclick="javascript:func(this)" ...

Using jQuery AJAX to preload GIF images for CSS background-image loading

For some time, I have been utilizing jQuery AJAX to preload GIF images with the following code: $("#images").html("<img id='loader' src='../img/ajax-loader.gif' />"); Now, I am attempting to achieve a similar effect (such as a s ...

Interaction when a button is clicked repeatedly 10 times on a webpage

Looking for assistance in creating a website feature where clicking a link ten times opens another webpage. Any help would be greatly appreciated. Thank you in advance! ...

Instructions on how to activate scrolling for a div element that becomes visible upon clicking a button

When a button in the navbar is clicked, a div appears with a height that exceeds the viewport/page size. How can I enable scrolling for the entire page when this happens? I attempted to use overflow: scroll, but it only applied scrolling to the internal d ...

Convert a JavaScript object into a serialized form

Alright, so here's the thing that's been bugging me. I have this JavaScript object that I need to pass to a PHP script using jQuery AJAX. But every time I try to pass it as is, I get an error. It seems like the object needs to be serialized befor ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Display data when clicking on Tailwind

I am currently displaying a sub menu on hover using Tailwind CSS. However, I am wondering how I can achieve the exact same functionality by triggering an onclick event instead of hovering over the menu. Here is a DEMO showcasing the current setup. CODE: ...

Adjust the Navbar to be Aligned Completely to the Left and Divide it into 12

I need my navbar to be split into 12 sections so that each item occupies one section, starting from the far left of the screen. The current alignment doesn't meet this requirement as shown in the image below; https://i.sstatic.net/irYqV.png Despite ...

The route you are trying to access is currently unavailable

I've successfully developed a web app, and now I need to replicate the same HTML page with a slightly different controller. Despite my attempts to utilize ngRoute, it's not functioning as expected, and I'm uncertain if my route setup will yi ...

What is the best way to conceal a parent element with jquery?

Let's say we have the following HTML structure: <li class="fooli"> <a class="foo" href="javascript:foo(this);">anchor</a> </li> <li class="fooli"> <a class="foo" href="javascript:foo(this);">anchor</a> ...