Should the element be scrolled beyond a fixed-positioned element

Is there a way to determine if an element is scrolled behind another element that is fixed at the top?

I am looking to trigger some javascript when an element is positioned below a fixed element and every height or scrollTop thereafter.

I am attempting to create a sticky header, but simply using fixed positioning based on scrollTop is not sufficient. This is because the element I want to make sticky gets pushed down as the viewport size changes, requiring a different scrollTop value to fix the element in place.

This has led me to consider the possibility of fixing/stickying the element only when it is underneath another element that is already fixed at the top.

So far, I have experimented with: position:sticky; along with its necessary vendor prefixes, along with trying out these two polyfills:

Answer №1

If you're looking for a solution to handle sticky elements on your website, Sticky Kit seems like the perfect tool for the job. You can check it out here:

One of the key features is the ability to trigger an event to recalculate the sticky positions when your content changes.

For instance:

$(window).on('resize', function(){
    $(document.body).trigger('sticky_kit:recalc');
});

Note:

I took a quick look at your website.

In order for this functionality to work correctly, you'll need to move your #filter and #main elements to a shared parent under <body> (not direct children of <body>):

<body>
    <div>
        <section id="filter">
            ...
        </section>
        <main id="main">
            ...
        </main>
    </div>
</body>

Then, in your main.js file, remove the entire $(window).scroll() handler as it's unnecessary. Let sticky-kit handle the calculations for the element's position:

// Removed $(window).scroll(...)
$('#filter').stick_in_parent();

$(window).on('resize', function(){
    $(document.body).trigger('sticky_kit:recalc');
});

Lastly, make sure to trigger the 'sticky_kit:recalc' event after any animations or effects such as slideToggle have completed. This will ensure that the fixed elements are positioned correctly. Failure to do so may result in strange behavior from the stickied element.

Best of luck with implementing this on your site!

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 alternative to using document.getElementById?

1) Question 1 Why does the following example work without using "document.getElementById('myId')" and is it acceptable to skip this step? <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Javascript quest ...

Is it Necessary to Create a Separate Stylesheet for This, And if so, How Should it be Implemented? (Using PHP

Creating a customized WordPress options panel is an essential task. One of the key functionalities it offers is allowing users to select their preferred colors. To achieve this, I currently follow a method where I utilize a default stylesheet and then appl ...

Tips for Adding Items to an Infinite Loop

Could you please review This Demo and advise me on how to continuously load items into the ul list in an endless manner? Currently, I have this code set up to display the list but I want it to keep adding new items to the end every time. var item = $(". ...

Create data according to jQuery's AJAX data method

When editing in place using jQuery, if the code snippet seems too complicated, please refer to a simpler one. 'column2' => "RAJANgsfdgf" 'column3' => "SRARDHAMgsdfgdf" 'column4' => "40043433" 'column7' =&g ...

React Router's default component for nested routes

In React Router, I am facing a challenge with nested Routes <Route path='about' component={{main: About, header: Header}}> <Route path='team' component={Team} /> </Route> Currently, the Team component is displayed ...

ReactJS Application: Issue with Selective Mobile Scrolling

I've been working on a ReactJS web app where we mainly use styled-components for styling, but also sometimes utilize index.css for global styles (such as body and html). The application consists of an app header, a page header, and a container with a ...

Error encountered while executing jest tests due to an unexpected import token

Despite trying numerous solutions and suggestions on Stack Overflow, I am still unable to resolve the issue at hand. I recently discovered jest and attempted to use it by following a tutorial on testing React components with jest from DZone. However, when ...

Sending a JSON Object to an API endpoint using the $.ajax method

While attempting to extract data from a form by clicking a button and sending it to a web method in my code behind, I am aiming to pass it as a JSON object, following what seems to be the convention. Below is the current code snippet that I have, but unfor ...

When attempting to use a jQuery-formatted JSON object within an <optgroup > returned via AJAX from PHP and MySQL, it appears to be incompatible with Select2

After spending an entire day attempting to create a PHP script that converts MySQL Query results into a JSON object for <optgroup> in Select2, I finally found a solution. However, when I implemented it, nothing appeared in the Select2 dropdown menu. ...

Display View with AJAX in the MVC pattern when server-side validation does not pass

I am currently working on a search form that involves client side validation, server action calls, and server side validation. This process can either return the original view with modelstate errors or display the search results. To achieve this functiona ...

Creating dynamic grid data based on various filter criteria using JavaScript (AngularJS)

In my approach, I have created two JSON data files. If the 'List' value equals A-1, I will retrieve the JSON data specific to that value. Otherwise, I will retrieve all the main data from data.json. My goal is to have 3 other checkbox options un ...

The NGINX reverse proxy fails to forward requests to an Express application

I am currently in the process of setting up a dedicated API backend for a website that operates on /mypath, but I am encountering issues with NGINX not properly proxying requests. Below is the nginx configuration located within the sites-enabled directory ...

altering the number of columns in Bootstrap based on screen size to prevent stacking

Is there a way to use the 'col' classes without having them stack on top of each other? I have a form row that requires different numbers of columns on various screen sizes. <div class="form-row"> <div class="col-3"></div> ...

Why is it necessary to decode JSON stringified objects in Vue props?

When passing a stringifyed object via props to a component, it seems like there is an issue with the data transformation. <my-component :filter="stringobject" ></my-component> stringobject = "{"search_text":"(ciCl ...

Is there a way to efficiently display more than 10 data items at a time using the FlatList component in react-native?

Here is the data I am working with: singlePost?.Comments = [ 0: {id: 82, content: "Parent1", responseTo: null} 1: {id: 83, content: "Child1", responseTo: 82} 2: {id: 84, content: "Parent2", response ...

Tips for adding a dynamic variable into a react JSX map function

I have a component that receives a props with the value of either 'A', 'B', or 'C' based on the selection made in the parent element. The challenge is to use this props to dynamically select an option list locally, instead of ...

Attempting to ensure that my website is fully optimized for all devices and

I am currently facing an issue with my CSS code on iPad. I want to change the background-image displayed based on whether the user is using an iPad or not. Specifically, I want to use headerold.jpg as the background-image for iPads while keeping headernew. ...

What is the method for obtaining an element with a class name that does not match a specific value?

I'm trying to figure out how to select an element with a class name that is different from the value passed in. For example: $(document).ready(function () { $(document).on('change', '#allRolesDD', function () { var toS ...

Why is the axios.get promise not getting resolved?

I am currently working on fetching data from MongoDB atlas within a React app. However, despite using .then and .catch with axios.get(), I am encountering an unresolved promise. Here is the code snippet: const entry = axios.get('http://localhost:3001 ...

Guide on presenting an image retrieved from a database with ajax

I am currently developing a straightforward mobile application that utilizes a database to store various types of content, including images. I have implemented ajax requests to retrieve this information and display it within the app. However, I am encounte ...