JS Nav Dots are not activating the Active Class

I have been utilizing a code snippet from this source to incorporate a vertical dot navigation feature into a single-page website. The navigation smoothly scrolls to different sections when a link is clicked, with an active highlight on the current section. I made several adjustments to the CSS to customize its appearance and then swapped out the sections.

However, I encountered an issue where the fourth and fifth dots do not react as expected - selecting the fourth dot also highlights the fifth one. This behavior occurs across multiple devices, unlike in the jsfiddle demo. On my Squarespace site, it appears like this. Any insights or assistance on what might be causing this discrepancy would be greatly appreciated.

You can access the Squarespace site through this link.

Here is the Jsfiddle link containing my code, along with the pasted JavaScript code below:

$(document).ready(function(){
$('.awesome-tooltip').tooltip({
    placement: 'left'
});   

$(window).bind('scroll',function(e){
  dotnavigation();
});

function dotnavigation(){

    var numSections = $('section').length;

    $('#side-nav li a').removeClass('active').parent('li').removeClass('active');     
    var sections = $('section');
    var i = 0;
    for (i = 0; i < sections.length; i++)  {

      var ele = $(sections[i]), nextTop;

      //console.log(ele.next().html());

      if (i < sections.length - 1) {
        nextTop = Math.floor($(sections[i + 1]).offset().top);
      }
      else {
        nextTop = $(document).height();
      }

      if (ele.offset() !== null) {
        thisTop = Math.floor(ele.offset().top - ((nextTop - ele.offset().top) / numSections));
      }
      else {
        thisTop = 0;
      }

      var docTop = $(document).scrollTop();

      if(docTop >= thisTop && (docTop < nextTop)){
        console.log(docTop + ":" + thisTop  + ":" + nextTop );
        $('#side-nav li').eq(i).addClass('active');
      }
    }
}

/* get clicks working */
$('#side-nav li').click(function(){

    var id = $(this).find('a').attr("href"),
      posi,
      ele,
      padding = 0;

    ele = $(id);
    posi = ($(ele).offset()||0).top - padding;

    $('html, body').animate({scrollTop:posi}, 'slow');

    return false;
});

Thank you in advance for any help you can provide!

Answer №1

It appears that the height of your last two sections is not large enough to be triggered at the top of the viewport. Consider setting the height to match or exceed the height of the window.

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

`Firefoxx border table glitch`

When attempting to open/close tables in Firefox using this link, unnecessary borders appear. One way to fix this issue is by implementing the following JS solution: setTimeout(function () { $table.css({'table-layout': 'auto'}) ...

Function compilation did not succeed in the module

I've put together a MERN (MongoDB, ExpressJS, React, Node) project using express-generator (express myNewApp). Within a react component, I have implemented this ES6 code snippet: onChange = (event, { newValue }) => { // Line 53 this.setSt ...

Showing a pop-up on a click of a dynamically created table row, showcasing information specific to that row

I am facing a challenge with my dynamically generated table that is based on the JSON response from an AJAX call. What I am trying to achieve is to display additional data in a modal when a table row is clicked. This would be simple if the data was hard co ...

HTML Automated Email

I am currently working on developing a WIFI landing page that requires users to input their email address in a textbox. Upon clicking 'Connect', the entered email address will be sent to another designated email for review. While I have found co ...

The method item.appendChild does not exist as a function

Despite being a common error, I've researched extensively and still can't figure out why it's happening. It seems like it should be an easy fix, but I'm struggling to find the solution on my own. var item = document.createElement("div" ...

What is a way to adjust the width of fixed columns in a Bootstrap 4.0 responsive table?

I've been struggling to make this work despite following similar questions and answers. I attempted to adjust the column width by directly setting each one, but nothing seems to change! <thead style="white-space: nowrap"> ...

Retrieve the 90 days leading up to the current date using JavaScript

I've been searching for a way to create an array of the 90 days before today, but I haven't found a solution on StackOverflow or Google. const now = new Date(); const daysBefore = now.setDate(priorDate.getDate() - 90); The result I'm looki ...

Unable to pass several parameters to a Component

I'm attempting to send three URL parameters to a React Component. This is my approach: App.js: <Route path="/details/:id(/:query)(/:type)" handler={DishDetails}/> DishDetails.js: class DishDetails extends Component { constructor(props) { ...

Highlighting of syntax within HTML elements marked with the <pre> tags

Is there a code library available that allows the display of code within <pre> tags while also highlighting syntax based on the programming language? For instance, displaying Python code like this: <pre class="python"> class MyClass: """A ...

Leveraging the back button in an AngularJS Mobile SPA

Utilizing AngularJS for my Single Page App (SPA), I am currently exploring the most effective way to handle the back button functionality on mobile devices. My setup involves a single JavaScript file, one HTML page, and no partials. The content is reveale ...

Unable to retrieve data with Selenium web driver

I have been attempting to extract the product description from a webpage using ID and tagname. However, when I try to print it out, the result is coming up empty. I am certain that I have used the correct locators to find the element. Page Source < ...

Template not rendering array data correctly in Meteor

Here is an example of the array structure: var myarray = [ device1: [ name:device1 , variables: [ variable1: [ name: variable1, unit: "a unit", ...

The icons from MaterializeCSS are not displaying correctly on the navbar within an Angular 7 project

Having an issue implementing MaterializeCSS Icons on the navbar. The arrow-drop_down icon is not displaying correctly, showing only text instead. Oddly enough, the icons render properly on other pages except for the app.component.html file. I attempted to ...

JMeter recording displays a script alert error notification

After recording the JMeter script on blazemeter, it initially worked fine on the website. However, upon running the jmx file, I encountered an error message: "alert("Something went wrong. Please try again");window.history.back();" I&a ...

Arranging my HTML text fields for optimal organization

I am struggling to format my text fields the way I envision. Here's what I want: - Username field at the top - First and last name underneath each other - Password and confirm password next to each other below names As a newcomer to HTML, I'm fi ...

I'm having trouble with my react-big-calendar not updating when I switch between day, month, or week views -

Why won't my calendar change to the week view when I click on that section? https://i.stack.imgur.com/gh2aO.png In the screenshot above, my default view is set to month, but when I attempt to switch to week, it only highlights the option without cha ...

Ensure that a string contains only one instance of a specific substring

I need a function that removes all instances of a specific substring from a string, except for the first one. For example: function keepFirst(str, substr) { ... } keepFirst("This $ is some text $.", "$"); The expected result should be: This $ is some tex ...

Retrieve the user_id without triggering any route

Is there a way to access the logged in user data without needing to make a request to any route or endpoint? //for example, how can I retrieve the id of the logged in user here? router.get('/',function(req,res,next){ //typically we would acce ...

Issue with jQuery validation plugin is that the select element is not being properly validated

The jQuery validation plugin I'm using (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) is functioning properly on most elements, but there are 2 select boxes where it's not working. You can see an example of one of these problema ...

Parallax scrolling within a div creates a dynamic visual effect

Hello there, thank you for taking the time to read this. I am currently working on a website and encountering some challenges while trying to implement a specific effect. I am aiming to achieve a parallax effect similar to what can be seen on this page. As ...