Shifting a div's position from relative to absolute causes it to revert back to its original

In my current project, I am experimenting with creating a floating table on the right-hand side of the screen while incorporating other content on the left.

To separate the left and right columns, I have utilized Bootstrap's class="col-md-6".

While making the table float is straightforward, I am facing the challenge of ensuring it stops floating when it reaches the bottom of the left column.

I tried implementing a solution found in this tutorial:

After following the tutorial, the floating div indeed changes to position:absolute; upon reaching the bottom. However, I encountered an issue where the table reverts back to the top position of the div instead of staying at the bottom. It only returns to floating when I scroll up slightly.

How can I prevent the floating div from jumping back up, similar to the behavior demonstrated in the JSFiddle link provided above? (http://jsfiddle.net/Kkv7X/)

Current HTML:

<div class="container">
  <div class="row" id="">

    <div class="col-md-6">
    Content for the left-hand column
    </div>

    <div class="col-md-6" style="position:relative;">
      <div class="positionfixed" id="fixedcard">
        <table class="table" id="recipetable">
          ...
        </table>
      </div>
    </div>

  </div>
</div>
<div id="stop">
</div>

Current JavaScript:

function checkOffset() {
    if($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10){
      $('#fixedcard').css('position', 'absolute');
    }
    if($(document).scrollTop() + window.innerHeight < $('#stop').offset().top){
      $('#fixedcard').css('position', 'fixed'); // restore when you scroll up
    }
  }
  $(document).scroll(function() {
    checkOffset();
  });

Current CSS:

 .positionfixed{
    position: fixed;
  }

You can view the current behavior of my code in this jsFiddle example: https://jsfiddle.net/fjfkbrtn/12/

Thank you for any assistance or insights you may provide.

Answer №1

When converting to absolute, simply adjust the CSS property bottom to 0px and revert it when switching back to fixed, as demonstrated below:

function monitorPosition() {
  if ($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10) {
    $('#fixedcard').css('position', 'absolute');
    $('#fixedcard').css('bottom', '0px'); // INCLUDE THIS
  }
  if ($(document).scrollTop() + window.innerHeight < $('#stop').offset().top) {
    $('#fixedcard').css('position', 'fixed');
     $('#fixedcard').css('bottom', 'initial'); // ALSO ADD THIS 
  }
}
$(document).scroll(function() {
  monitorPosition();
});

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

Dynamic reloading of a div with form data using jQuery's AJAX functionality

I am currently developing an online visitor chat software using PHP and MySQL. My goal is to load the page when the submit button is clicked. Submit Button ID: send Visitor ID: vid Chat ID: cid Below is the snippet of code for an Ajax request that I hav ...

In React-bootstrap, is there a way for a custom class to take precedence over the btn and btn-danger classes without relying on IDs or !important, and only utilizing

I have been using React-bootstrap to style my buttons, however, I am facing difficulty in overriding the default btn and btn-danger classes provided by Bootstrap. Is there a way for my custom class slotTiming-box-button to take precedence over the btn and ...

The Navbar Items Background Color is not functioning as anticipated

When the hamburger is clicked on mobile devices, I want my navbar items to have a full background-color display that only shows the logo, the hamburger icon, and the navigation items. The main content of the landing page should be hidden unless the hamburg ...

Issue with Bootbox alert causing focus to not return to the input box

For my latest project, I decided to upgrade from standard alert and confirm boxes to the more stylish bootbox. However, I encountered a problem where the focus does not return to my input field after closing or dismissing the bootbox. bootbox.alert("P ...

Imitate a hover effect

In my list, I have images and descriptions set up in the following format: <li> <img class="photography" src="PHOTO/boat.jpg" alt="Boat on sea." /> </li> <li><div id="description" class="description"> <p>BOAT</p> ...

Submitting the form without utilizing Ajax, but instead sending data directly to a PHP script

I've encountered an issue while posting a form to a PHP file using AJAX. Despite my efforts, the form is bypassing AJAX and posting directly to the PHP file. Here is my form: <form id="editform" name="editform" action="ajaxeditform.php" method= ...

Looking for a simple way to update the text in a textarea based on the selected option in a <select> dropdown using JavaScript

Desperately seeking assistance - I have a dilemma. The textarea data on my website needs to be altered when the user selects a different option from a dropdown menu. Is there a simple JavaScript solution you can offer to achieve this? I would greatly app ...

How to use AngularJS to showcase intricate data structures

Here is the JSON data that I have: { "_id": "543e95d78a1cec2a38ed53ec", "result": { "CAR009": [ { "name": "BMW" }, { "name": "MERCEDES" } ], "BUS007": [ { ...

Font display is malfunctioning within a WordPress theme

I am currently in the process of updating my online portfolio and have encountered an issue with @font-face. Initially, everything was working perfectly fine as I implemented it in static HTML. However, when converting the static HTML into a dynamic Wordpr ...

Hosting images with an online service on jsfiddle: A guide

Exploring the world of HTML canvas and honing my skills in HTML and CSS through jsfiddle, I am eager to incorporate my own images. While sites like Google Drive provide free image hosting, I wonder if I can utilize them with drawImage(); Is there a way to ...

What is the best way to add child elements to the parent element when the height limit has been reached?

This is the code I've been working on: <ul style="height:200px;width:100%;"> <li>Hi</li> <li>Hi</li> <li>Hi</li> ... (add more items here) </ul> Here is a preview of how it currently looks: ...

What might be stopping Javascript from saving the value to the clipboard?

On my ASP.Net website, I am incorporating basic Javascript functionality. One particular task involves calculating a value and saving it in a hidden textbox, like this: <asp:LinkButton ID="LinkButtonShare" runat="server" CssClass="btn btn-success" OnC ...

Codacy's eslint configuration file

While utilizing Codacy for code quality monitoring, I'd like to have a preview of what Codacy will detect before pushing any changes. It's apparent that Codacy incorporates eslint and other tools for this purpose. Is there a possibility to genera ...

You can see through the menus as if they were made of

I'm in need of assistance to understand why the pull down menus (buildings, BV Mail) on certain pages are appearing as transparent while on others they are not. The website address is www.bv340.com. Feel free to ask if you require any additional det ...

Tips for checking a worldwide event tracker in VueJS

Learn how to implement a global event bus in VueJS with this insightful article. Instead of the conventional method of using an event bus defined in a separate file, the article introduces an alternative approach: import Vue from 'vue'; const E ...

The InfoWindow on Google Maps is positioned above the Marker

My struggle lies in positioning the infowindow above my Google Maps API marker, as the infowindow keeps hiding the marker. I attempted using CSS to adjust the position of both elements, but unfortunately, it didn't have the desired effect. Here is th ...

Activate scroll function exclusively upon hovering over specific object

I am working on a page with an object that allows users to zoom in and out. I want to create a special zoom function that will be triggered when the user scrolls while their cursor is hovering over this object. If the cursor moves away from the object, th ...

Etiquette for the organization of jQuery functions through chaining and nesting

For developers familiar with jQuery, creating dynamic HTML easily can be done using the following method: var d = $('<div/>') .append('some text') .append( $('<ul/>').append( $('&l ...

Using Javascript to dynamically enable or disable an input field depending on the choice made in another field

I attempted to implement the solution provided in this answer for my code: How do I disable input field when certain select list value is picked but unfortunately, it is not working as expected. The scenario involves an HTML select field with the ID &apos ...

Having trouble with document.getElementById.innerHTML not displaying the correct content?

document.getElementById works in getting the element, but for some reason, the content disappears as soon as it is written in it. There are no errors on the console to indicate what's causing this behavior. I have been unable to identify the reason b ...