Tips for keeping a div fixed at the bottom of the page when scrolling after a checkbox has been selected

Typically, I don't ask questions until I've exhausted all other solutions...but here we are.

Despite my efforts in searching, I have not found any similar questions to mine.

So, my question is: How can I make a div at the bottom of the page sticky after selecting a checkbox, only returning to its original position when the user scrolls back up?

I came across an example on the Kickstarter website that demonstrates this feature: I wish I knew how they achieve it :)

If my explanation was unclear, please let me know so I can provide further details.

Thank you in advance

Answer №1

Once the checkbox is selected,

Feel free to apply the following CSS code to the div element

position:fixed; bottom:0;

Answer №2

Looking to make an element have a fixed position at the bottom of its container when checked

HTML

<div class="wrapper">
    <input type="checkbox" id="check"/>
    <div id="foot"></div>
</div>

JavaScript

var check = document.getElementById('check');
var foot = document.getElementById('foot');

check.addEventListener('change', function () {
    if (check.checked) {
        foot.style.position = 'fixed';
        foot.style.bottom = 0;
    }
});

JSFiddle link - http://jsfiddle.net/qak2ept6/

Update - http://jsfiddle.net/qak2ept6/1/ restore position when unchecked

Additional Update - http://jsfiddle.net/qak2ept6/3/ attach on scroll

Answer №3

By selecting the checkbox, a fixed position div is generated and the offset of the window's bottom edge is saved as the default window height. A scroll event is assigned to continually monitor if the scroll value matches the stored offset. Once it reaches this point, the fixed positioning on the div is removed.

Answer №4

In my opinion (and if I were the one doing it), the best approach would involve tracking the scroll position and dynamically adjusting a css style as needed.

Here is a potential solution:

  1. Start by injecting the element into the document in an invisible state
  2. Take note of its initial position on the page (y coordinate)
  3. Add a class to make it stick to the bottom of the window and become visible
  4. While scrolling, once the element reaches the expected y coordinate, remove the sticky class and let it flow normally in the document
  5. If the user continues to scroll past the element, reapply the sticky class until they return to view it

I hope this helps!

Answer №5

It seems like you are looking for a solution related to sticky elements. If that's the case, you can check out an example here

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}
$(function () {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});

If this is not what you were looking for, please provide more details and code so we can better assist you.

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

jQuery ajax issue: Unable to locate file using relative path

I have recently transferred some script from an HTML document to an external file. I updated the jQuery path to point to the new location of the JavaScript file, but now it is unable to locate the necessary PHP file. Can anyone shed light on why this might ...

Utilizing Web Worker to Load Textures in Three.js

When a large texture image is applied to a Mesh in Three.js for a substantial amount of time, it causes the browser's main thread to freeze. Consider the following scenario: var texLoader = new THREE.TextureLoader(); texLoader.load('someLargeTe ...

What is the reason behind Ramda having multiple curry functions in its source code, much like Redux having multiple compose functions?

There are _curry1, _curry2, _curry3, and _curryN functions within the depths of Ramda's source code This interesting pattern is also utilized in the redux compose function I find myself pondering: why did they choose this intricate pattern over a mo ...

Generating PDF files utilizing AJAX technology with FPDF

I have a PHP script that generates a PDF file using the FPDF library. When I run the script directly, it successfully creates and opens the PDF in the browser. However, when I try to trigger this generation process through a button click using AJAX, it doe ...

Having trouble with NPM Moment.js in React: Why is moment__WEBPACK_IMPORTED_MODULE_2__format not functioning properly?

scenario, After resolving my current issue using dateFns (date-fns.org), I am interested in integrating Momentjs into the project as well. The task at hand involves converting data to a string format within an app built with create-react-app. However, wh ...

Checking for the existence of a date in an array and retrieving its corresponding ID using AngularJS

I need to find the ID associated with a specific date in an array. The code I have tried using includes() is not working as expected. const results = []; angular.forEach(getEventdate, function(value) { results.push({id:value.id,event_date:value.event_dat ...

Is it advisable to store JSON data in data params instead of making an AJAX call?

Previously, I stored my component data within the element's data parameter. Should I continue doing this or switch to using an Ajax call to fetch the data? 1. Using data parameter: <a href="#" data-core="{JSON-data}" id="item-1"> <a href ...

arranging and organizing a collection of objects

So I have this array of objects: const users= [{ Location: { label: "aa", value: "aa" }, Name: "test7" id: "002" }, Location: { label: "aa", value: "aa" }, Name: "test4" id: "003" }, { Location: { label: "ss", value: "ss" }, Name: "test4" ...

The global function is not recognized within the Angular controller script

I am facing an issue with calling a global function within an Angular controller. Here is the scenario: initiateCheckList = {}; $(function() { initiateCheckList = function() { ... } Inside an Angular controller, I have a function that tries ...

What is the best way to show a scrollbar within a container?

Is there a way to show a scrollbar inside a container when its content overflows, similar to how Facebook notifications display? When using overflow:auto, the scrollbar is shown outside the container. Is there a method to render the scrollbar within the c ...

Tips on retrieving an item using jQuery's select2 plugin

How can I retrieve the value (flight_no) from the processResults function in order to populate the airline name with the respective flight number when selected? I've attempted to access the (flight_no) within the processResults function, but I'm ...

Vue.js application returning 404 error when trying to include a script in index.html

After generating my application with a simple command: vue init webpack I decided to install jquery from the node_modules folder: npm install --save jquery and then added it to my index.html page like this: <head> <script src="node_module ...

How can we stop the page from scrolling after a click?

Upon clicking the link labeled Click Me, the page automatically scrolls back to the top. This behavior is not desired. How can I correct this issue? For example, here is a link to a relevant resource: http://jsfiddle.net/Y6Y3Z/ Regarding the scroll-bar: ...

What can you do to ensure Vue detects input element changes after using preventDefault?

In this example, I am trying to prevent a newline from being created in a textarea when the Enter key is pressed. To achieve this, I use the preventDefault method in my event handler and update the value inside the textarea. However, I encounter an issue w ...

Troubleshooting a bug in React TypeScript with conditional logic

I have a new message button and a few conversations. When I click on a conversation, the right side should display the message box. Clicking on the new message button should show the create new message box. Here are my useState and handlers: const [newMess ...

Best method for connecting user input with a class

I'm currently working with a WYSIWYG editor (Tinymce) that allows users to post YouTube videos. In order to make the video content responsive, I need to add the class "video-container" around any "iframe" tags inserted when users paste a YouTube link ...

Activating a div in React.js using setActive: Step-by-step guide

Currently, I am in the process of developing an application with three menu tabs, each represented by an accordion comprising a menu title and content. The issue I am facing is that when clicking on any menu title, all content divs are displayed simultaneo ...

Creating dynamic props for Element UI components can be achieved by utilizing Vue's re

I am currently customizing an Element-UI datepicker component to meet my specific requirements. I am looking to introduce one or more props to set a default value. pickerOptions: { type: Object, default: () => ({ addedProp: '', de ...

Unexpected error occurred when attempting to fetch the jQuery value of the radio button: "Unrecognized expression syntax error"

I am facing an issue while trying to extract the value of a radio button using $("input[@name=login]"); I keep getting an "Uncaught Syntax error, unrecognized expression" message. To view the problem, visit http://jsfiddle.net/fwnUm/. Below is the complet ...

Serialization of JSON is not possible for the data type <code>[object Promise]</code>

Full error: Error: Issue when serializing data .b retrieved from getStaticProps in "/". Cause: object ("[object Promise]") cannot be serialized as JSON. Please ensure only JSON serializable data types are returned. Encountering an er ...