Ensure that jQuery waypoints become unstuck upon reaching the footer area

Currently, I have successfully implemented jQuery waypoints for a sticky social media navigation. The only issue is that when the footer element is reached, the sticky navigation continues to scroll. My ideal scenario would be for the sticky nav to remain at the bottom of its parent container (.content) once it reaches the footer. Likewise, if the user scrolls back up, the sticky nav should become sticky again.

Is there a straightforward method to accomplish this? Check out the example on JSFiddle.

var sticky = new Waypoint.Sticky({
  element: $('.sticky')[0]
});

Answer №1

To ensure the sticky div behaves correctly as it reaches the footer, you must add another waypoint in the footer. This waypoint will remove the .stuck class when the sticky div is about to reach the footer (set up with the offset option). The .stuck class makes the div fixed until the footer allows it to be displayed again. Here's how you can achieve this:

$('.footer').waypoint(function(direction) {
    $('.sticky').toggleClass('stuck', direction === 'up')
}, {
offset: function() {
    return $('.sticky').outerHeight()
}});

Please verify if this meets your requirements by visiting: https://jsfiddle.net/elbecita/mna64waw/3/

UPDATE: I almost forgot one crucial detail!! You also need a class for the sticky div when the footer passes over it. Therefore, the final JavaScript code would look like this:

$('.footer').waypoint(function(direction) {
    $('.sticky').toggleClass('stuck', direction === 'up');
    $('.sticky').toggleClass('sticky-surpassed', direction === 'down');
}, { offset: function() {
       return $('.sticky').outerHeight();
}});

The styles for .sticky-surpassed should be as follows:

.sticky-surpassed {
    position:absolute;
    bottom: 0;
}

For the latest update, click here: https://jsfiddle.net/elbecita/mna64waw/5/

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

Implementing Vue.js - Applying a Class on Click Behavior

Is there a way in Vue.js to dynamically toggle a class on a click event for a div without relying on properties or data? Take a look at this div: <div class="quiz-item" @click="checkAnswer(1)"> Upon clicking this div, I want to add either the clas ...

Can someone guide me on developing a similar design utilizing HTML canvas with HTML 5 technology?

Hey everyone, I could really use some assistance on creating a design with Html 5 canvas. My current issue is that the rectangles I'm trying to generate are overlapping and not fitting properly within the canvas. Below, you'll find both a referen ...

javascript show and hide navigation bar

I am currently working on a HTML menu that includes a button to open it and an unordered list : <nav class="menu"> <button> <h1>Menu</h1> </button> <ul class="mylist" ...

JavaScript AJAX function is returning an undefined value rather than a boolean true or false

My AJAX call function in jQuery has a complete section with the following code: complete: function(XMLHttpRequest, textStatus) { if(textStatus == "success") { return(true); } else { return(false); } } However, when ...

How can I rename attribute values in a dropdown menu and ensure they work properly?

I'm facing an issue with my code. How can I store the option values in a database when they have numbering like value="1" or value="2"? Can I change it to something like value="1000" and have the other select box change to value="250" when selected? ...

Jumping CSS dropdown menu glitch

I'm facing an issue with a dropdown menu. The main problem is that the "parent" link is moving when hovered over. HTML: <ul id="nav"> <li><span>Page 1</span> <ul> <li><a>Extralong Pag ...

AngularJS - Choose and establish initial values for Editing or Creating New Items

My first project involving AngularJS has left me a bit stuck when it comes to using the select list. I need to either set the default value to the first option for a new entry, or if it's an edit, select the appropriate value. In my form, there are t ...

Implementing a Searchable Autocomplete Feature within a Popover Component

Having an issue saving the search query state. https://i.stack.imgur.com/HPfhD.png https://i.stack.imgur.com/HbdYo.png The problem arises when the popover is focused, the searchString starts with undefined (shown as the second undefined value in the ima ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

The traditional function does not have access to a reference to this

For my web development project with Angular 9, I needed to add a typeahead feature using ng bootstrap typeahead. The code provided below worked perfectly: search = (text$: Observable<string>) => text$.pipe( debounceTime(150), disti ...

Acquiring information from a different Vue.js component

I am faced with a puzzle involving 2 components, A and B. Component A: import B from '../components/B.vue'; export default { components: { B }, methods: { test: function() { console.log(B.data().settin ...

Sort through categories using PHP and MySQL for an organized search

Hello, I have a search code that I am using: <form action="search.php" method="get"> <input type="text" name="query"> <select name='category'> <option value="all">All categories</option> <? while ($cat = mys ...

Adding auth0 authentication to a Next.js 13 application: A step-by-step guide

Currently, I am using a nextjs 12 application and set up auth0 as my authentication provider by following the guidelines provided here: https://auth0.com/docs/quickstart/webapp/nextjs/interactive. However, I am now looking to upgrade my application to next ...

Creating numerous strings using template literals

I am looking to create multiple strings using a template literal and an array variable. For instance, a template literal allows you to replace an expression with its content in a string: var = "world"; tpl = `Hello ${var}!`; console.log(tpl); // Hello wor ...

Optimize your bootstrap carousel for mobile viewing by ensuring that the image and text are

My bootstrap carousel is displaying perfectly on desktop screens with images sized at 1200x400. However, when viewed on mobile devices, the images shrink so much that the text on them becomes unreadable. Additionally, the navigation dots at the bottom of t ...

What could be the reason for the background image not displaying?

Below is the HTML code I have: <div class="content-wrapper"> <div class="center"> <h1>title</h1> </div> <div class="lr"> <p> text </p> </div> </div> The CSS for the content-wrapp ...

Transmit information using express handlebars in a straightforward node application

Struggling to pass data from express handlebars to index.html? Check out my code below: server.js code: const express = require('express'); const app = express(); const expressHandlebars = require('express-handlebars'); const path = r ...

Add values to an array using the reduce function

My array consists of multiple objects: const items = [{ search_type: 'environment', search_code: 'TBA_ENVIRONMENT00002', asset_code: 'ASSET00002' }, { search_type: 'job', search_code: 'TBA_JOB0000 ...

capture the mouse click event on a particular object within the renderer

I have set up a canvas renderer with two mesh (cubes) and I am trying to capture the click event on each cube in order to execute the appropriate method for it. Currently, I am able to capture the click event on the entire renderer, so when I click on cub ...

Challenges with Angular 4 service initialization

Having trouble with my authentication service. The constructor is being called 259 times when making an HTTP request, but only once when the call is removed. I am using a shared module to provide a unique instance of the service. Angular version: 4.4.4 C ...