Can someone assist me in completing a Vertical Dots Navigation feature that activates on scroll?

I'm struggling to create a vertical dots navigation that dynamically adds the "active" class based on the section currently in view while scrolling.

For example, if you are on the first section, the first dot should be white and the rest transparent. As you scroll to the second section, the second dot turns white, the first one light grey, and so on.

If the background of a section is white, the active class should change to blue.

This is what I have attempted so far: https://jsfiddle.net/wx38rz5L/2075/

  
    // Attempting to add active class based on Scroll
    var top1 = $('#firstproject-header').offset().top;
    var top2 = $('#firstproject-team').offset().top;
    var top3 = $('#firstproject-about').offset().top;
    var top4 = $('#firstproject-lorem').offset().top;
    var top5 = $('#firstproject-contact').offset().top;

    $(document).scroll(function() {

          if ($(document).scrollTop() >= top1 && $(document).scrollTop() < top2)  {
            $('.scroll-fixed a:first').addClass('active');
            $('.scroll-fixed a:last').removeClass('active');  
          } else if ($(document).scrollTop() >= top2 && $(document).scrollTop() < top3) {
              $('.scroll-fixed a:first').css('background-color', '#00f');
              $('.scroll-fixed a').css('background-color', '#0f0');
          } else if ($(document).scrollTop() >= top3 && $(document).scrollTop() < top4) {
            $('.scroll-fixed a').css('background-color', '#00f');
          } else if ($(document).scrollTop() >= top4 && $(document).scrollTop() < top5) {
            $('.scroll-fixed a').css('background-color', '#222');
          }
           else if ($(document).scrollTop() >= top5) {
            $('.scroll-fixed a:last').addClass('active');
            $('.scroll-fixed a:first').removeClass('active');   
          }
    });

While I managed to add the active class based on click with a smooth animation, I'd appreciate some assistance with adding the blue and white active classes when scrolling through sections.

Any help would be greatly appreciated.

Answer №1

Take a look at this JSFiddle. It took a lot of effort to get everything fixed, but I believe it's all sorted out now.

You were making a mistake when using

$('.scroll-fixed a:first').css('background-color', '#d3d3d3');

The correct way to do it is like this

$('.scroll-fixed li:first a').css('background-color', '#d3d3d3');

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

Update the text within a textarea by clicking with the power of jquery

Is there a way to clear the content of my textarea on click? Below is the code snippet: http://jsfiddle.net/QMfyP/ <textarea name="adventage" style="width:400px; border-radius: 3px; border-left: 4px solid #6C3" id="adventage" cols="45" rows="5">Sh ...

"Transcribe as HTML" for embedded IFrame components within Chrome's Inspector tool

When using Chrome's web inspector, there is a helpful "Copy as HTML" option when you right-click on a DOM element. However, it seems that this feature does not include the contents of IFrame tags, even though they are displayed in the inspector as chi ...

Extracting pictures with py Jupyter

While attempting to scrape images from this URL using Python in Jupyter: https://www.adobe.com/products/catalog.html?sort=name&types=pf_252Fdesktop&types=pf_252Fmobile&types=pf_252Fweb&page=1, the code I ran resulted in the following error: ...

Can HTML code be saved in "context" and then utilized in one of the templates?

I am striving to extract the code of a table from another website using selenium, save it in the context dictionary, and then embed it into one of the templates so that it seamlessly becomes part of the page's HTML structure without displaying the cod ...

The selection feature is not supported for the type of input element in jQuery

I'm currently attempting to utilize AJAX to send a form with jQuery. However, upon clicking the submit button (which is technically a link), I encountered an error. The error displayed in the console is as follows: Failed to read the 'selection ...

Issues with Media Queries on Desktop Devices

I've encountered an odd issue with my media queries. While they function properly on my mobile devices, resizing my desktop browser doesn't seem to trigger the changes. This prevents me from inspecting my mobile styles using web inspector/firebug ...

The CORS error I receive states "Access Denied"

I created a custom SharePoint app that uses Jquery to call a Webservice hosted on an IIS server. The SharePoint Server, specifically the app, will be hosted within the SharePoint domain at: https://mysharepointapp-3b9352b780820d.mydomain.com/sites/... H ...

When attempting to load a page in Mozilla, the initial DIV code fails to load

When trying to load a page in Mozilla, the first DIV code is not loading. I attempted to load HTML inside using: <div id="mainContent"></div> with the following call: if (destinationURL != null) { $.get(destinationURL, function(data) { ...

Issues with retrieving the output of a function nested within another function through an ajax request

I am attempting to use jQuery to add the results of an ajax call to a paragraph. My goal is to extract the "myResult" variable from the inner getResult function and transfer it to the outer buildParagraph function. Unfortunately, the returned value is und ...

Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs. Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote. I&ap ...

What is preventing me from using AJAX to input data into sqlite?

Implementing a local save feature in my Web App is proving to be quite challenging. Every time I attempt to send data in any form, I consistently encounter the following error: A builtins.TypeError occurs, followed by a stack trace in /SaveFile and jquery ...

Using the <video /> tag on a Next.JS page generated on the server side leads to hydration issues

My Next.js app's index page is rendered on the server side by default. During development, I encountered an error that stated: Unhandled Runtime Error Error: Hydration failed because the initial UI does not match what was rendered on the server. Wa ...

Using Selenium to interact with a checkbox in a React component's ::before pseudo-element

I've been attempting to select a checkbox, and here are some of the methods I have tested: //label[string()=('Notify user')]/i //div[@class='b-checkbox'] //div[@class='b-checkbox']//label//input[@type='checkbox&apo ...

Clarification: Javascript to Toggle Visibility of Divs

There was a similar question that partially solved my issue, but I'm wondering if using class or id NAMES instead of ul li - such as .menu, #menu, etc. would work in this scenario. CSS: div { display:none; background:red; width:200px; height:200px; } ...

trouble fetching ajax information

Help needed with ajax data retrieval issue on different browsers: Success in IE 7/8 Failure in FF 3.5/3.6, Chrome 5.0.375.70 The jquery code snippet used is pretty basic: $.get('http://myhost/someurl',function(data) { if (data) { ...

Display sub navigation when clicked in WordPress

I currently have the default wordpress menu setup to display sub navigation links on hover, but I am interested in changing it so that the sub navigation only appears when the user clicks on the parent link. You can view my menu here https://jsfiddle.net/f ...

Step-by-Step Guide: Building a PHP-powered AJAX Notification System for Friend Requests

I want to develop a unique notification system using AJAX and incorporate some cool Web 2.0 features. With my PHP expertise, I aim to notify users in real-time when their friend request is accepted by $username. This will be achieved through an interacti ...

The animation function in A-frame causes entities to vanish when used with D3.js

Working with the animation property in D3.js has been a bit of a challenge for me. When I include the a-animation directly in the HTML section, everything works as expected. <a-box id="box" position="0 1 0" rotation="0 0 0" scale="1 1 1" color="#4CC3D9 ...

Looking for search suggestion functionalities in an HTML input field

I am currently working on a website project and have a database filled with recipes to support it. The issue I am facing is related to the search feature on my webpage. At the top of the page, there is a textarea where users can input their search queries ...

What is the best way to display value in a dropdown option field using Laravel?

I am currently working with 3 tables: hosts, visitors, and visitor_types. My objective is to display the host name and visitor type in a drop-down option button. However, I find myself a bit perplexed when it comes to the controller and route code. I have ...