Floating sidebar dynamically overlaps with the footer as you scroll

Currently, I am utilizing Bootstrap for my website layout and facing an issue with making the sidebar fixed on scroll after it reaches a certain point in the viewport. The scrolling functionality works fine initially, but when it reaches the bottom, the sidebar starts overlapping the footer section. Despite trying multiple solutions from various sources, I have not been able to resolve this problem.

The objective is to stop the sidebar at the end of its main parent container without overlapping the footer section.

To see the issue in action, you can visit the following Codepen link: https://codepen.io/zakero/pen/yLgBGqq

Below is the code snippet:

// FIXED SCROLL 
const main = $("#main");
const aside = $('#aside-content');
const asideInner = $('#aside-content .aside-content');
const sticky = main.offset().top;
$(window).scroll(() => {
  if ($(window).scrollTop() >= sticky) {
    $(aside).addClass("aside-fixed")
  } else {
    $(aside).removeClass("aside-fixed");
  }
});

// PREVENT OVERLAP ISSUE
// const topOfFooter = $('.footer-bs').position().top;
// const scrollDistanceFromTopOfWin = $(window).scrollTop() + // $(tableOfContentsInner).height();
// const scrollDistanceFromTopOfFooter = scrollDistanceFromTopOfWin - topOfFooter;
// if (scrollDistanceFromTopOfWin > topOfFooter) {
//  $(tableOfContents).css('margin-top', 0 - scrollDistanceFromTopOfFooter - 48);
// } else {
//  $(tableOfContents).css('margin-top', 0);
// }

.header-content {
  background-color: blue;
  height: 800px;
  margin: 20px 0;
}

.main-content {
  background-color: cyan;
  height: 2000px;
  margin: 20px 0;
}

.aside-content {
  background-color: red;
  height: 400px;
  margin: 20px 0;
}

.aside-fixed {
  position: fixed;
  top: 0;
  right: 0;
}

footer {
  background-color: brown;
  height: 400px;
}

Answer №1

It is possible to achieve this using only CSS and no JavaScript:

`
.box{
  display:block;
}
#sidebar-section{
  position: sticky;
  top: 0;
  align-self: flex-start;
}
`

Answer №2

Make sure to include z-index: -1; in the .aside-fixed class.

In CSS, the z-index property determines the stacking order of elements with non-static positions. A higher z-index value means the element will be placed on top of elements with lower z-index values.

By setting a z-index: -1; for your aside-fixed element and assuming that the footer's position is static (defaulting to z-index: auto; which is essentially z-index: 0;), the footer will overlay the aside-fixed element.

For better organization, it is recommended to assign explicit z-index values to all relevant elements to avoid conflicts. This approach helps in avoiding negative z-index values and ensures clearer code structure.

To learn more about z-indexes, you can refer to the MDN Web Docs: z-index

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

The correct rendering of background image paths is not functioning properly

Currently, I am utilizing Django Compress to streamline the process of using fewer files in their original format, rather than converting them to CSS files. This method has been successful except for a minor issue with translating the paths for background ...

The processing of the PHP email validation function is not functioning

I've been working on a contact form code, but I'm having trouble validating the email field. When testing with incorrect data, the filter_var($email, FILTER_VALIDATE_EMAIL) function doesn't seem to work as expected. To keep things simple and ...

An error was encountered regarding an undefined property while executing NPM and PACT

I'm currently working on implementing the PACT workshop example with some different data. Although this might be more of a Javascript/Node query, I am a bit stuck as a beginner. Here is a snippet from the consumer.spec.js file: const chai = require ...

Obtaining the domain from a cookie using AngularJS

I've encountered a issue with removing cookies from my browser when logging out. It seems that I need to specify the domain in order to remove them correctly. $cookies.remove('Name',{domain:'.test123.com'}); My goal is to automat ...

Callback after completion of a for loop in Angular TypeScript

During the execution of my javascript async function, I encountered a situation where my printing code was running before the div creation. I needed to ensure that my print code would run only after the completion of the for loop, but I struggled to find a ...

How can event.target.value and event.target.checked be used simultaneously in React.js?

I have a combination of input fields including text and checkboxes. What is the best way to retrieve all the field values together? Currently, I can only get one value at a time as shown in the example below: handleChange = event => { this.setState( ...

Flexibility on smaller screens using Flexbox

On larger screens, I have arranged 4 icons in a row, but on smaller screens (less than 768px), I need to display only 2 icons in a row. This is my current code : .welcome { display: flex; justify-content: space-around; } @media (max-width: 768px) ...

The full background width is not being displayed as 100% on the

Unexpectedly, the "full left secondary-bg" background division on this particular page (http://goo.gl/OU4MkW) is no longer spanning the full width of the screen and I am unable to figure out why. This website is WordPress-based and constructed using the sk ...

Can you explain the significance of 1x, 3x, etc in the Karma code coverage report for Angular unit testing?

I am a beginner when it comes to Unit Testing in Angular. Recently, I set up karma with code coverage using angular-cli. After running the command ng-test, I checked out the code coverage report and noticed references like 1x, 3x, etc. next to my code line ...

Maintain line breaks in the scanner input within an HTML element

Currently, I am utilizing a Zebra DS9908 scanner to scan a barcode and transfer the data onto an HTML page. However, I am encountering an issue with preserving all input characters. Despite attempting both a <div> and <textarea>, the line feed ...

Updating the database using Codeigniter and jQuery without requiring a page reload or redirection

I have been attempting to update a database row without the need to refresh or redirect the page in order to commit the query. Despite trying everything I could think of, I have not had any success. On my gallery page, when a user submits an artwork, it is ...

Is there a way to seamlessly update a field without refreshing the page or overloading the server?

I'm intrigued by the concept of updating a field dynamically without refreshing the page or overwhelming the server with queries. Stackoverflow demonstrates this feature when someone answers our question, and it instantly shows at the top of the page. ...

v-if causing inability for bootstrap dropdown to expand

Encountered a strange issue with a v-if statement in relation to a sidebar dropdown menu. Essentially, I am trying to hide the dropdown menu based on user permissions. The problem arises when the v-if statement is added - the dropdown menu fails to work pr ...

What is the best way to prioritize loading JSON models first in three.js and declare a variable before initializing?

I am looking to efficiently load multiple JSON models and store them in global variables for easy access. This will streamline tasks like copying, translating, and more without the need to reload a model each time. After trying various methods, I have not ...

What is the best way to locate {Tags} only within the visible HTML content, excluding the elements' attributes?

Is it feasible to extract a specific pattern using only regexp (and possibly php) that falls outside of one pattern but within another in php (or maybe with ?R in php regexp for the recursive aspect)? The objective is to locate the tags between > {This ...

Retrieve from MongoDB the items where the age is greater than 10 using the find function in the learngyoumongo

Currently working my way through the learnyoumongo tutorial and facing a challenge in part 3. The task involves a test database filled with parrots, and the objective is to retrieve the parrots whose age exceeds a specified input value. Despite using Mongo ...

Cloning the final section with jQuery: Step-by-step guide

Can someone help me figure out how to modify my jQuery code so that it only clones the last section? I want the next click to add: <section id="unit4">cc</section> JQUERY $("#addUnit").click(function () { // Clone the last SECTION v ...

Tips for controlling the size of a canvas element: setting minimum and maximum width and height properties

function convertImageResolution(img) { var canvas = document.createElement("canvas"); if (img.width * img.height < 921600) { // Less than 480p canvas.width = 1920; canvas.height = 1080; } else if (img.width * img.he ...

How can I duplicate an element twice in AngularJS, without having them appear right after each other?

Within my AngularJS template html file, I am faced with a dilemma regarding an html element: <div>This is a complex element that I want to avoid typing multiple times</div> My challenge is that I need this element to show up twice on my websi ...

What are the steps for integrating a CMS with my unique website design?

Currently, I am in the process of creating a unique website for a client using my own combination of html, css, and JavaScript. There is also a possibility that I may incorporate vueJS into the design. The client has expressed a desire to have the ability ...