An adhesive feature that halts upon reaching a specific element

Can anyone help me create a fixed element that behaves like a sticky element when scrolling and reaches the top of another specified element? I want this fixed element to stay within the boundaries of the element I set as the "ground", preventing it from passing beyond that point. Check out this pen I created to demonstrate what I am looking for: https://codepen.io/vendramini/pen/xNWpPK. I'm unsure about the specific calculations needed to achieve this effect, so any assistance would be greatly appreciated.

If you would like to see a visual representation of my idea, please visit the following link again: https://codepen.io/vendramini/pen/xNWpPK.


*{
  margin: 0;
  padding: 0;
}

section{
  height: 100vh;
  width: 100vw;
  background: #eee;
  position: relative;
  max-width: 100%;
}

.a{
  background: #faa;
}

.b{
  background: #ffa;
}

.c{
  background: #afa;
}

.d{
  background: #aaf;
}

.sticky{
  width: 100%;
  position: fixed;
  height: 100px;
  background: blue;
  opacity: 0.5;
  bottom: 0;
  z-index: 1;
}

.ground{
    height: 2000px;
    background: black;
}
//jQuery required

(function($){

  $('[data-bound]').each(function(){

    const $elem = $(this);
    const $bound = $( $elem.data('bound') );

    $(window).scroll(function(){

      const scrollTop = $(window).scrollTop();
      const boundTop = $bound.offset().top;
      const boundHeight = $bound.height();
      const delta = (scrollTop - boundTop); //+ boundHeight;

      console.log({
        scrollTop,
        boundTop,
        delta,
      });

      if( delta > 0 ){
        $elem.css('bottom', delta);
      }
      else{
        $elem.removeAttr('style');
      }

    });


  });

})(jQuery);

<div class="sticky" data-bound="#ground"></div>

<section class="a"></section>
<section class="b"></section>
<section class="c"></section>
<section class="d"></section>
<footer class="ground" id="ground"></footer>
<section class="a"></section>
<section class="b"></section>
<section class="c"></section>
<section class="d"></section>


I am aiming to achieve a fixed element that stays within the bounds defined by the ground element without exceeding it. Your support in achieving this goal is much appreciated.

Answer №1

I'm not entirely certain about your desired outcome, but it seems possible to achieve using only CSS by implementing the position: sticky property on the footer.

https://codepen.io/anon/pen/jozzPq

Here are the necessary adjustments:

Begin by wrapping the elements containing the sticky footer:

<div>
  <section class="a"></section>
  <section class="b"></section>
  <section class="c"></section>
  <section class="d"></section>
  <footer class="ground" id="ground">   </footer>
</div>

Position the footer at the bottom and apply the sticky property:

.ground{
    height: 100px;
    background: black;
    position: sticky;
    bottom: 0;
}

Review the codepen as a significant amount of CSS and potentially all JS can be eliminated.

Answer №2

After much searching, I have finally uncovered the solution:

https://codepen.io/vendramini/pen/xNWpPK

To solve the issue, it is necessary to factor in the height of the window when calculating the delta:

const windowHeight = $(window).height();
const delta = (scrollTop - boundTop) + windowHeight;

I am grateful to everyone who contributed to this discussion!

Answer №3

Switch out

      if( delta > 0 ){
        $elem.css('bottom', delta);
      }
      else{
        $elem.removeAttr('style');
      }

for

$elem.css('bottom', 0);

in order to keep the element fixed at the bottom.

Answer №4

I'm looking for something similar to what UIKit offers, but with a twist:

However, the issue lies in the fact that UIKit employs top positioning instead of bottom.

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 confirm only one click per browser?

Is there a way to limit rating functionality to allow users to rate only once per browser session? I am looking to implement validation for a rating component in React where users can only submit their rating once per browser. Below is an example of the ...

Tips for arranging five images side-by-side in a row

I have a collection of 5 images: Image 1: Image 2: Image 3: Image 4: Image 5: The dimensions of each image are as follows: Image 1: 70x40 Image 2: 80x42 Image 3: 90x44 Image 4: 100x46 Image 5: 120x48 I would like to arrange these images ...

JavaScript code utilizing Selenium to retrieve text from a Tinymce text editor

When using https://ocr.sanskritdictionary.com/ to upload an image, the copyable text appears in a Tinymce editor. I am looking for suggestions on how to copy the resulting text using Selenium JavaScript code. I have attempted to extract it using the html ...

Change the background color to be visible when there is text overflow with CSS

When I set my <P> tag with a fixed height and overflow:hidden, the text is displayed. However, when I apply a hover effect to it, I set overflow:visible; The issue I am facing is that the text is displayed, but I would like to adjust the z-index and ...

Switch it up on Radio Select by showcasing a variety of checkbox options

I am in search of a solution using either Javascript or jQuery. My requirement is that when a user selects an option from the radio buttons, a different set of checkboxes should be displayed. For example, if the user chooses "By Name", then the name check ...

Unable to save session cookie in browser when accessing server from different domain

I am encountering an issue with the browser not saving the session cookie sent from my server. The client web app is hosted on Vercel, while the server app is hosted on Heroku. I utilize passport for signing in, and upon successful authentication, a sessio ...

Having trouble with obtaining real-time text translation using ngx translate/core in Angular 2 with Typescript

Issue : I am facing a challenge with fetching dynamic text from a JSON file and translating it using the translate.get() method in Angular2. this.translate.get('keyInJson').subscribe(res => { this.valueFromJson = res; /* cre ...

What could be the issue with this JSON data?

After referencing this stackoverflow thread on how to return success/error messages for JQuery .ajax() using PHP, I implemented the accepted answer in my PHP script. I am returning the following json with correct headers: Content-type: application/json and ...

Issue with Repeated String Test in JavaScript: Single Failure Detected

Currently tackling the Repeated String Challenge on HackerRank. The challenge description goes as follows: A string, denoted by s, consisting of lowercase English letters is repeated infinitely. With an integer, n, the goal is to determine and output the ...

Filling a Textbox with pre-selected options from a Dropdown menu

I have a dropdown list that has been populated from my database, along with three textboxes. Now, I need help retrieving data into the textboxes from the database based on the selected item in the dropdown using JavaScript. If anyone could assist me with ...

Ways to assign 'selected' to a dropdown option that matches the value received from an ajax response?

My goal is to have the select option automatically 'selected' based on the value returned from an ajax response. array $array=array("Price 1","Price 2","Price 3","Price 4","Price 5"); I've been pondering about looping through the array un ...

Configuring route for serving static files in an Express server

I'm completely new to working with express, but I am eager to learn and follow the best practices. My goal is to serve files like CSS or index.html from a folder called 'public'. I have seen examples using .use and .get methods as shown belo ...

In Javascript, the scrollTo function will only be triggered when the entire page is

I've hit a roadblock with this issue, trying every solution I could find online to no avail. My challenge involves using the jquery plugins scrollTo() and localScroll() for animating scrolling on various pages of my website. The problem arises when t ...

Using Javascript, retrieve a custom attribute specific to a checkbox option

How can I access the custom attribute "mem_name" value? <input class="messageCheckbox" type="checkbox" value="3" mem_name='ABC' name="checkmember" > <input class="messageCheckbox" type="checkbox" value="1" mem_name='PQR' name ...

Javascript code has been implemented to save changes and address resizing problems

Code Snippet: <script> function showMe(){ document.querySelector('#change').style.display = ''; document.querySelector('#keep').style.display = 'none' document.querySelector('#change1').style.d ...

How can I stop and hover over time in AngularJs Interval?

Within my UI, I have a time element that is continuously updated using AngularJS Interval. Even the milliseconds are constantly running. Is it possible to implement a feature where the time pauses when hovering over it? Any assistance would be greatly appr ...

Transpiling Sccs/Sass code into CSS

Is there a way to create a compiler that can convert SCCS/SASS code into CSS without using a VSCode extension? I'm curious about what programming language and basic concepts are needed for this task. Any insights would be greatly appreciated. Thanks! ...

How to extract various elements from a database array by utilizing .map within a React Component?

I'm currently diving into React and have encountered a challenge when it comes to rendering multiple elements simultaneously. The issue revolves around fetching an Array from Firebase, which is supposed to generate a new div and svg for each item in ...

MongoDB has encountered an issue where it is unable to create the property '_id' on a string

Currently, I am utilizing Node.js and Express on Heroku with the MongoDB addon. The database connection is functioning correctly as data can be successfully stored, but there seems to be an issue with pushing certain types of data. Below is the database c ...

Enhancing SVG G elements with d3.zoom for better scaling and transforming effects on bigger children

I'm facing an issue where the shapes generated inside a G element are larger than the container. Despite trying different methods, most examples I come across have hardcoded sizes and coordinates. Even when attempting to calculate offsets, the output ...