Convert a fixed Jquery div to absolute positioning when it reaches the end of the page

I've implemented a Jquery code that adds a fixed class to a div when scrolling down. However, I now want to remove this fixed class when it reaches the bottom of the page and replace it with a new one that includes position:absolute; and margin-bottom:200px;. This adjustment is necessary to prevent the box from overlapping with the footer at the end of the page once the scrolling is completed.

To get a better understanding of what I'm trying to achieve, you can view the JsFiddle file here: http://jsfiddle.net/hcb4v21r/

$(document).on("scroll",function(){
    if($(document).scrollTop()>150){ 
        $("#item-nav").addClass("fixedProfileNav");
        }
    else{
        $("#item-nav").removeClass("fixedProfileNav");
        }
    });

I initially thought that modifying the code in this way would suffice, but it turned out to be ineffective:

$(document).on("scroll",function(){
        if($(document).scrollTop()>80%){ 
            $("#item-nav").removeClass("fixedProfileNav");
            }
        else{
            $("#item-nav").addClass("fixedProfileNavBottom");
            }
        });

CSS

.fixedProfileNavBottom{
    position:absolute;
    margin-bottom:200px;
}

I was considering removing the fixed class when the scroll position reaches 80/90 percent of the page, but I am uncertain if this approach is correct.

Answer №1

It may be beneficial to update the code snippet like this:

if($(document).scrollTop()>80%){ 

to

if($(document).scrollTop()>(window.innerHeight * 0.85)){ 

or

if($(document).scrollTop()>($(document).innerHeight() * 0.85)){

or

if(($(document).scrollTop() + window.innerHeight)>($(document).innerHeight() * 0.85)){

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

What is the best way to initiate an animation once the one before it has finished?

I am facing an issue with my animation function where all animations play simultaneously. This is not the desired behavior; I would like to play each animation after the previous one ends. Can someone guide me on how to achieve this? canvas = document.get ...

Can you provide instructions on creating a small border at the center of an h1 heading?

What is the best way to create a small border in the center of an h1 text element? ...

What's the deal with jQuery and Intraweb?

Does anyone know how to create a jQuery notification or popup calendar in Intraweb? I would really appreciate a simple example. If possible, I'd love to see a version using Dojo as well. ...

Issues with Bootstrap columns not displaying correctly in versions 5 with col-sm-6 and col-md-4

When using Bootstrap 5, the col-sm-6 and col-sm-4 classes do not function correctly on small and medium screens, but work fine on large screens. The goal is to display 2 images on small screens and 3 images on medium screens. However, only 4 images are dis ...

Guidelines for implementing a seamless translation effect with CSS3

CSS: .horizon { position: absolute; top: 380px; width: 1800px; height: 50px; background: url(images/road.png) repeat-x; -webkit-animation-name: prop-600; -webkit-animation-duration: 20s; -webkit-animation-iteration-count: i ...

Having Trouble Styling Radio Buttons with CSS

Hello, I'm facing an issue with hiding the radio button and replacing it with an image. I was successful in doing this for one set of radio buttons, but the second set in another row is not working properly. Additionally, when a radio button from the ...

What are the steps to decode a JSON response using jQuery?

Working on a fun little web app for my significant other and me to keep track of movies we want to watch together. I'm exploring using TheMovieDatabase.org's API (which only supports JSON) to simplify the process of adding movies to our list. The ...

The autocomplete feature in the hotel name field is designed to recognize HTML-encoded entities, thanks to the combination of

I keep encountering this issue. "King & Grove Hotel" is displaying as "King & Grove Hotel" This problem arises while using jQuery autocomplete. ...

Ajax request fetching distinct data from database

My regular ajax function successfully posts to a php page and retrieves data from a table. However, I encountered an issue when attempting to set the user's FB image. The error message I received was: "error": { "message": "Unsupported get reques ...

The overlay background is not covering the entire height of the container

The CSS includes a .wrapper with the style min-height: calc(50% - 30px), along with an overlay blur div to cover the entire height of the .wrapper element. Here's a link to a Codepen example showcasing this setup: https://codepen.io/palaniichukdmytro/ ...

Executing PHP code from a list item

On one of my website pages, I have a list that functions as a delete button. However, I am interested in making it so that when a user clicks on the delete option, a php script is triggered - similar to how a submit button works. Below is the list structu ...

Schedule Picker

Looking to incorporate a calendar datepicker into my website. Is this achievable and if so, how can it be done? Here is an example: http://fullcalendar.io/js/fullcalendar-2.6.1/demos/agenda-views.html. Thank you. ...

Adjust the fade-in effect to transition from a white hue to a custom color

Is there a way to customize the color of the fadeIn() effect from its default white to a specific color? For example, when transitioning the entire webpage fades in from grey and fades out to grey when a link is clicked. Here is my current code snippet: ...

Anyone faced the issue of jquery_ujs corrupting the callback URL in Rails 4, causing problems with updating screens via Ajax?

Encountering a Catch-22 situation with jQuery in my Rails app - when I include jquery_ujs, it pulls the wrong URL from Rails during execution, leading to quiet errors when attempting to access non-existent URLs on the server. However, if I omit jquery_ujs ...

Validation of time picker consistently returns false

While using the daterangepicker library for my form validation with JavaScript, I encountered an issue with the time field. It returns false and displays an error message saying: "Please enter a valid time, between 00:00 and 23:59," preventing me from addi ...

What is the best way to add several icons at once?

Is there a way to insert multiple star icons directly below the review text? I attempted to use pseudo elements to add the icons, but I could only insert one icon when I actually need to include multiple icons. Here is what I have tried so far: .review:: ...

Django CSS graphical interface for selecting styles

I am looking to implement a feature that allows users to select an "interface theme": To enable users to change the theme across all templates, I have created a dropdown in my base.html. For all my HTML templates, I utilize a base.html file by extending ...

Is it possible to adjust the height of the navbar in Bootstrap?

Currently, I am in the process of replicating the mac OS navbar and am seeking guidance on resizing the navbar height and adjusting text size to around 12px. This is my first time working with bootstrap, so any assistance would be greatly appreciated. Addi ...

block height has become a constant challenge for me - despite my various attempts, I still cannot seem

Struggling with adjusting the height of my posts. Any help would be appreciated! For more details, please visit: <li> <div class="thumb-img"> <a title="Hiring a Professional Designer for Your Kitchen" href="http://www.mulberrydesignerkitc ...

Error 419 Encountered on Laravel 5.6: Revised and Corrected

Having a Laravel 5.6 setup with PHP 7.2 across multiple pages was all smooth sailing until this morning when I switched to another computer. Despite having CSRF tokens in place and fetching them for AJAX as per the documentation, everything seemed fine unt ...