Modify anchor link color in a one-page website by using Jquery to detect scroll position changes when reaching different page sections

Each if statement contains specific numbers to change the text color of the visited/current/active anchor link. The height of the page is dependent on its width. How can this be resolved? Apologies if my explanation is confusing, English isn't my strongest language XD

Codepen

var bottomPage = $(window).scrollTop() + $(window).height() == $(document).height();
var scrollPos = $(window).scrollTop();

if(scrollPos >= 300 && scrollPos <= 854){
    $('header nav ul li a').removeClass();
    $('header nav ul li a[href="#about-nav"]').addClass('current');
}else if (scrollPos >= 855 && !bottomPage){
    $('header nav ul li a').removeClass();
    $('header nav ul li a[href="#portfolio-scroll"]').addClass('current');
}else if (bottomPage){
    $('header nav ul li a').removeClass();
    $('header nav ul li a[href="#contact-scroll"]').addClass('current');
}else {
    $('header nav ul li a').removeClass();
}

Answer №1

Finally cracked it!

Behold, my latest masterpiece of code. I have a feeling this one is flawless.

var bottomPage = $(window).scrollTop() + $(window).height() == $(document).height();
var about = $('div#about-nav').position();
var aboutPos = about.top;
var portfolio = $('div#portfolio-scroll').position();
var portfolioPos = portfolio.top;

if(scrollPos >= aboutPos && scrollPos < (portfolioPos - 1)){
$('header nav ul li a').removeClass();
$('header nav ul li a[href="#about-nav"]').addClass('current');
}else if (scrollPos >= (portfolioPos - 1) && !bottomPage){
$('header nav ul li a').removeClass();
$('header nav ul li a[href="#portfolio-scroll"]').addClass('current');
}else if (bottomPage){
$('header nav ul li a').removeClass();
$('header nav ul li a[href="#contact-scroll"]').addClass('current');
}else {
$('header nav ul li a').removeClass();
}

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

"Creating Search Engine Optimization friendly URLs: A Step-by-Step

Looking for ways to optimize my dynamic URL! http://www.example.com/hamlyn/page.php?id=14 Any suggestions on shortening or making it more search-engine-friendly? ...

Toggle the visibility of the identical div

I am facing a challenge with dynamically hiding and showing various div elements on my website. I have managed to achieve this using show/hide, but the issue arises when I need to show/hide some of the same fields repeatedly. The script works fine on the f ...

Unable to activate dropdown menu in html and css

Hi there, I'm facing an issue with hiding the dropdown-menu submenu. I tried using display: block !important;, but it didn't work. Even when I used display: none !important;, the dropdown menu still wouldn't disappear. I want the dropdown s ...

When Django comments go wrong: Issues with Ajax and CSRF verification

Having an issue here that seems a bit different from what others have encountered. I've gone through various answers but still no luck. Appreciate any assistance: I've got a list of News items resembling a Facebook feed, and each one has a comme ...

Struggling with integrating JSON data into JqGrid

I've encountered a challenge with binding my Json data to a JqGrid. Within my Default.aspx.cs file, I have the following method: [WebMethod] public static string GetData() { CustomerHelper C = new CustomerHelper(); ...

Leveraging the GetListItems method via an Ajax request in SharePoint 2010

I'm currently working on implementing an ajax call to fetch list items from SharePoint by utilizing the Lists.asmx service. Despite following the correct formatting, I keep encountering a persistent 302 error. Is there something crucial that I may be ...

Achieving a perfect circle can be accomplished by setting both the width and height to the same

Trying to make the height of an element equal to its width For example, aiming for a perfect circle Although the console displays equal values, the resulting height is noticeably greater than the width $('button').on('click', funct ...

Cheerio doesn't exit the loop prematurely

The Cheerio/JQuery documentation mentions that using return false in a loop should terminate it early. Below is the code snippet in question: "use strict"; let cheerio = require('cheerio'); let $ = cheerio.load('<a href="www.test.com"&g ...

Issue with JQuery closest() selector functionality not working

Currently, I am working on a small coffeescript that will allow me to delete a list from HTML. Included below is a snippet of the sample HTML: <li class="picture col-xs-3" id="picture_38"> <div class="thumbnail"> <img src="/system/upload ...

Integrating Python Script with User Input and Output within a JavaScript Web Application

I have an existing JS website that requires additional functionality, and after some research I believe Python is the best tool to handle the necessary calculations. My goal is for users to input information that will then be used as input for my Python ...

Trouble with X-editable linking to database for updates

Utilizing the X-Editable plugin within my PHP application to update fields in a table and utilizing a POST file to update the database. Below is the form code: <table id="restaurant" class="table table-bordered table-striped"> <tbody> ...

Populating a Dual ListBox with JSON data

I'm currently using a dual listbox control from and am attempting to populate it with information retrieved from a JSON request. Initially, I populated the Duallistbox using a VB sub like this: Public Shared Sub GenerateDropDownListAndValues(dt As ...

Ensuring Form Submission is Successful: A Guide to JQuery/Ajax Form Validation and the Importance of Validating Forms and Ajax Operations

I’m currently facing an issue with the form validator I'm using and need help resolving it. The form validator in question can be found at http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/. My problem ...

Ways to extract text from HTML elements that have been rendered using jQuery

Utilizing jQuery (AJAX), I am fetching data which then populates a table on the web page after it has already loaded. Within each row of the table, there is a 'select' link that allows users to pick a specific row. The goal is to extract the inf ...

Sorting feature fails to function properly when used in combination with pagination and

<table> <thead> <tr> <th class="col-md-3" ng-click="sortDirection = !sortDirection">Creation Date</th> </tr> </thead> <tbody> <tr dir-paginate="item in items | filter:itemFilter | items ...

Tips for retrieving text without a class or id using selenium and python

I am attempting to extract a list of variables (date, size, medium, etc.) from the following webpage using python/selenium: Getting the titles was straightforward with: titles = driver.find_elements_by_class_name("sln_lot_show") for title in titles ...

Is there a way to dynamically modify the text of an HTML button element with jQuery?

My goal is to modify the text value of an HTML code using jQuery... <div class="dropdown"> <button type="button" class="btn btn-secondary dropdown-toggle button-text" data-toggle="dropdown><span>1395</span></button> ...

The xmlHttp.getResponseHeader method is currently ineffective with CORS

My ASP.NET WCF on .NET 4 is used for user authentication. When submitting a username and password, an HTTP Header should be returned with the authentication cookie included. The process works correctly when accessing a locally hosted test page. However, I ...

Need assistance with Jquery? Get your customized slickquiz to display image behavior

I recently utilized the slickquiz plugin in one of my projects, where I incorporated images for each question. However, I am facing an issue and seeking guidance on how to delay the image transition until the next question is loaded instead of it changing ...

Loading message displayed in web browsers by banner rotation system

I'm currently working on a banner rotator function that seems to be showing a "loading" message continuously while running. Here is the code snippet for reference: var banners = ['../Graphics/adv/1.gif','../Graphics/adv/2.jpg']; / ...