Ways to activate a hyperlink when scrolling to the bottom of a page

I'm working on a one-page layout and trying to figure out how to make the "Contact Us" section stand out when it's active. I want it to have a blue background like the other links do when clicked and scrolled to.

One idea I had was to make the "Contact Us" section active when the page is scrolled to the bottom. I've been experimenting with some JavaScript code, but so far, it hasn't quite done the trick.

UPDATE:

I'm making progress! I've added some jQuery code and included specific classes in my HTML for the links, however, I've noticed that the "Get A Quote" link doesn't activate until I scroll past it and then back down. Any suggestions on how to fix this?

Revised HTML Code

<li class="quoteive">
                    <a href="#quote">Get A Quote</a>
            </li>
            <li class="contactive">
                    <a href="#contact">Contact Us</a>
            </li>

Updated JavaScript Code

$(window).on("scroll", function() {
        var scrollHeight = $(document).height();
        var scrollPosition = $(window).height() + $(window).scrollTop();
            if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
                    $(".contactive").addClass('active');
                $(".quoteive").removeClass('active');
            }

            else    {
                $(".contactive").removeClass('active');

            }
    }); 

Original Code:

(Original CSS and HTML code omitted for brevity)

Answer №1

Here's a straightforward CSS solution using the Relative position with negative top and padding adjustment.


#contact {
  z-index: -1;
  position: relative;
  top: -200px;
  padding-top: 200px;
}

By applying this to the #contact container, it will shift up while maintaining its content in place.


Note: Feel free to modify the pixel value according to your needs, and remember to set a lower z-index for the container.

Example:

(JavaScript code snippet here)
(CSS styles here)

EDIT:

You can implement this JavaScript function to monitor scroll positions and apply active classes dynamically to elements based on visibility.

(More JavaScript code here)

This is how you can use HTML markup to leverage the above functionality:

(HTML code snippet here)

If you want the switch to happen slightly before reaching the bottom of the page, simply adjust the condition in the script accordingly.

Check out this working demo

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

Generate a dynamic nested JSON structure in real-time

(context)I am currently gathering data from various elements to create a JSON object that will be passed down to an MVC3 controller for deserialization. Within the JSON object, there are both 'items' and 'item settings'. At the moment, ...

Sharing data from ViewModel to JavaScript

Currently, in the process of creating an MVC 5 web application, I am faced with the challenge of passing a value from my ViewModel to a function in my JavaScript code. The objective is to hide a Label/Div element when the user selects the option "No" and ...

Is there a way to retrieve the text content from a span element using JavaScript?

Revised for better organization and explanation of the question Hello, I am new to web programming so please bear with me. My question is about extracting customer information from the following code snippet: <span class="tag-element-content" ...

Responsive design image scaling in Firefox on CentOS Linux is essential for optimizing the visual experience of

I am experiencing an issue with an arrow inside a circle in CSS: img { float: left; width: 15%; max-width: 33px; height: auto; } When viewing the image in Windows XP (IE8, Safari, Opera, FF, Chrome), everything appears ...

Compare the versions of React used in the linked library and the workspace application

As I work on developing a React library containing my commonly used components, I have organized my project structure in the following way: In the root folder, there is my rollup config file and the src/ folder which houses my library. Upon building, the ...

Working with three.js: Implementing an external texture in a glTF file

In my quest to apply a texture file to a 3D model using three.js, I've hit a roadblock. Despite days of research and numerous attempts, I just can't seem to get it right. Here is the method I've been using to set the current object in the sc ...

When attempting to update a file using AJAX after it has been modified, Chrome displays the error message net::ERR_UPLOAD_FILE_CHANGED

I am facing an issue with a basic HTML form that utilizes AJAX to send a selected file to an API endpoint. The problem arises when I follow these steps: Click on the Upload button and initiate a POST request to upload the file Modify the selected file usi ...

What is the best way to replace an image in a div using Angular?

After studying an example of a directive here, I noticed that they used <a href="#" ... to insert a new image into the container div. I attempted to replicate this functionality in my own directive, but clicking on my images does not change the content ...

Choose a specific category and its corresponding subcategory from the drop-down

Is it feasible to implement a category in the drop-down menu using jQuery? For example: Southern Suburb suburb1 suburb2 suburb3 suburb4 Northern Suburb suburb5 suburb6 Appreciate your help! ...

Leverage the power of forkJoin in JavaScript by utilizing objects or sourcesObject

I'm currently facing an issue with my code snippet below: getInformations().subscribe( informations => { let subs = []; for (const information of informations) { subs.push(getOtherDetails(information.id)); } ...

Implemented smooth scrolling for anchor links on website but encountering trouble accessing external links now

I am encountering an intriguing issue on my website. If you visit the URL provided below and follow the instructions, you will see the problem: Here are the steps to replicate the issue: Load the website In the navbar menu, select any link to trigger a ...

Transforming button properties into a JSON format

I am currently in the process of developing a web application using node.js and mongodb. Within my app, there is a table that dynamically populates data from the database using a loop. I encountered an issue with a delete function that I implemented base ...

Tips for integrating google's api.js file into my vue application

Currently, I am in the process of integrating Google Calendar into my Vue project. The steps I am following can be found at this link. Additionally, I came across an example code snippet at this URL. This is how my Vue file looks: <template> <d ...

Creating an installation package for an Electron application on Windows

Is it possible to create a Mac installer for an Electron app using a Windows PC? I have tried running npm make, but it only generates a Windows installer. ...

The Node express GET route experiences intermittent failures when using wildcard match for every other request

I have configured my router to accept any GET request that does not include the word "API" in it. This is specifically for my single page application to handle bookmarked URLs that are not recognized by the app's routes. However, I am encountering an ...

Delay execution of Selenium WebDriver until the element appears on the screen

After scouring Google and the SO site, I was able to find answers for JAVA but not for node.js. I have a web application that has a slow loading time. I want the selenium program to wait until the page is fully loaded before taking any actions. Below is ...

The HTML marquee element allows text to scroll sideways or

Has the html marquee tag been phased out? If so, what are the current alternatives available for achieving a similar effect on modern browsers? I am looking to implement a basic marquee effect on my Joomla page. ...

The Angular Google Maps Directive zooms in too much after the "place_changed" event has fired

Currently, I am developing a store locator app for DHL accessible at storefinder.hashfff.com/app/index.html For this project, I decided to utilize the angular-google-maps library for its features. However, in hindsight, working directly with the Google Ma ...

Error: The JSON data contains an unexpected token "p" at the beginning. This error occurred during the

const displayQuote = document.querySelector(".quotes"); fetch(`http://quotes.rest/qod.js?category=inspire`) .then(function(response) { return response.json(); }) .then(function(myJson) { console.log(JSON.stringify(myJson)); }); error message d ...

What is preventing AngularJS from being invoked in Microsoft Dynamics CRM 2015 on IE11?

I created a ribbon button to open an HTML page from my web resources. After adding an example.html page with code borrowed from W3 schools, I encountered an issue: <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...