Using jQuery to create a fixed wrap element

I have a fixed wrap div on my page that I want to reposition after scrolling. My attempt using jQuery resulted in the div being too high and overlapping the footer.

img1

img2

Snippet from my code:

Below is the revised code snippet:

var elementPosition = $('#fixed-wrapper').offset();
$(window).scroll(function() {
  if ($(window).scrollTop() > elementPosition.top) {
    $('#fixed-wrapper').css('position', 'fixed').css('top', '0').css('margin', '20px 1%');
  } else {
    $('#fixed-wrapper').css('position', 'static');
  }
});
#header {
width: 101%;
padding: 10px 0px 0px;
margin: -10px -10px 10px -10px;
background-color: rgba(0, 0, 0, 0.7);
display: table;
min-width: 700px;
}

#main-bets{
display: table;
float: left;
width: 68%;
margin-left: 7%;
margin-top: 20px;
margin-bottom: 30px;
min-width: 900px;
max-width: 900px;
background-color: rgba(0, 0, 0, 0.5);
}

#fixed-wrapper {
    display: table;
    float: right;
    width: 22%;
    right: 5px;
    margin: 20px 1%;
    max-width: 300px;
}

#footer {
width: 101%;
padding: 10px 0px 0px;
margin: 20px -10px -10px -10px;
background-color: rgba(0, 0, 0, 0.7);
display: table;
clear: both;
}
<div id="header">
    ...
</div>

<div id="main-bets
    ...
</div>

<div id="fixed-wrapper">
....
</div>

<div id="footer">
...
</div>

Answer №1

If you're looking to achieve that effect, check out my awesome sticky float jQuery plugin (take a look at the demonstration page), or explore the modern CSS feature: position:sticky (please note it's not compatible with older versions of Edge/IE)

Answer №2

Take a look below:

body {padding:0; margin: 0;}

#header {
    width: 101%;
    padding: 10px 0px 0px;
    margin: -10px -10px 10px -10px;
    background-color: rgba(0, 0, 0, 0.7);
    display: table;
    min-width: 700px;
}

#main-bets{
    float: left;
    width: 68%;
    margin-left: 7%;
    margin-top: 20px;
    margin-bottom: 30px;
    min-width: 900px;
    max-width: 900px;
    background-color: rgba(0, 0, 0, 0.5);
}

#fixed-wrapper {
    float: left;
    background: red;
    width: 22%;
    right: 5px;
    margin: 20px 1%;
    max-width: 300px;
}

#footer {
    width: 101%;
    padding: 10px 0px 0px;
    margin: 20px -10px -10px -10px;
    background-color: rgba(0, 0, 0, 0.7);
    clear: both;
}

//---------------- HTML ---------- //

<div id="header">
    Header
</div>

<div id="main-bets">
    Bets
</div>

<div id="fixed-wrapper">
    Wrapper
</div>

<div id="footer">
    Footer
</div>

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

Eliminate the see-through effect from a logo positioned in a fading container

I created a div with image fading functionality using JavaScript and placed a static SVG logo on top of it. The issue I'm facing is that the logo appears somewhat transparent, allowing the image in the div to show through. Despite adjusting the z-inde ...

Executing various axios requests to retrieve diverse data and populating multiple sections of the user interface in React Native

I am struggling to display various categories of movies on the same screen, such as "POPULAR MOVIES", "RECOMMENDED MOVIES", and "NEWEST MOVIES". I have been able to retrieve data for the "POPULAR MOVIES" section using an API call, but I'm unsure of th ...

Real-time database on Firebase website encountering an error despite the correct path being specified

excerpt from the import section: ... import { ref, getDownloadURL, deleteObject } from "firebase/storage"; import { child, get } from "firebase/database"; ... This is the function code snippet: function getDirectLink() { console.lo ...

Determine the potential width of an HTML SPAN within a parent element of unlimited size?

I'm looking to determine the width of a particular SPAN element in Javascript or jQuery, based on how wide it would be if its parent was infinitely large (its "preferred" width). What is the most efficient way to accomplish this task? ...

Guide to leveraging the jotform API for form integration

Seeking a method to display forms created in JotForm using APIs. The goal is to make an API call, pass an ID, and retrieve the required information from JotForms to render the created form in JotForm. After researching, I haven't found a suitable sol ...

Having difficulty navigating to a new tab when clicking the mouse middle button or using Ctrl+click in AngularJS

Currently, I am working on an Angular 1.6 application and facing an issue with the code I implemented from stackoverflow. It's not solving my problem. Here is the code snippet: app.run(function($rootScope, $state) { $rootScope.navigate = function($ ...

Steps to trigger an action upon selecting a radio button on an HTML and CSS website

After creating a website with HTML and CSS that allows users to select options using radio buttons, I am now seeking advice on how to implement actions based on their choices. If a user selects an option, I want a specific action to occur, but I am unsur ...

Display error messages upon submitting the form

I am currently working on an Angular 6 Form with validation. My main goal is to display error messages only after the form has been submitted. It is crucial that these messages remain constant while the user types in the input field. For instance, if a use ...

Guide on incorporating Bootstrap 4 beta.2 into an Angular 4 project

After attempting to install Bootstrap 4 version beta.2 along with its dependencies (jquery and popper.js), I encountered a strange problem. A SyntaxError message kept appearing in the console, specifically stating "SyntaxError: export declarations may only ...

Tips for including vertical lines within text boxes to distinguish elements

I've been working on a simple form using CSS and HTML, but I'm struggling to add vertical lines to separate my text from the radio buttons. Right now, I'm just using '|' to divide them, but it's not the most visually appealing ...

Customizing nvd3 chart colors with CSS: A step-by-step guide

Currently, I am faced with a challenge in SugarCRM as I try to modify the colors of nvd3 charts using CSS. The pie chart colors are structured differently compared to other nvd3 charts, making it difficult for me to figure out how to customize them through ...

Is my JSON data causing the error of invalid React child components?

Although this question has been asked multiple times on Stack Overflow, the solutions provided have not worked for me. My main goal is to retrieve notifications from a JSON file located here: I suspect that my JSON file may be poorly structured, especial ...

Error: Attempting to access property of undefined variable

I have searched for similar titles related to my issue, but I still haven't found a solution. The error I am encountering when clicking on agent_dashboard on the side panel is as follows: txtDisplayName = <div style="border:1px solid #990000;paddi ...

Experimenting with ngModel in a Jasmine test suite

I've created a directive that generates input fields and connects them to the scope using ngModel. When a user uses this directive in the HTML file like so: <div tk-quick-form="formStructure" form-data="formData" id="main_form" ></div> a ...

What is the most effective method for executing functions in a step-by-step manner, starting from 1,

var container = $("div"); firstFunction(), secondFunction(), thirdFunction() function firstFunction() {container.css("background", "#f00").hide().fadeIn(1000)}; function secondFunction() {container.css("background", "#ff0").hide().fadeIn(1000)}; function ...

There was an issue with locating the module: Error: Unable to find the specified module 'vue-axios' in the directory 'C:xampphtdocsvue-laravel-api esourcesjs'

I am currently working on a CRUD project using Laravel and Vue.js. I made sure to install all the necessary devDependencies, but when I try running NPM run watch, I encounter the following error: https://i.sstatic.net/Kqkpq.png My app.js file includes all ...

JavaScript Tutorial: Updating the maximum value of a gauge chart

Is there a way to dynamically change both the total and max values in my current code? The maximum value needs to be dynamic based on the filter applied. Here is the code snippet: resource1=[ "//cdn.jsdelivr.net/raphael/2.1.0/raphael-min.js", "//cd ...

right-aligned dropdown toggle

Is there a way to align the dropdown-toggle to the right in a bootstrap dropdown? My code and a jsfiddle are provided below. The arrow pointing down is currently next to the text, but I would like it on the right side. Check out this JSFiddle for referenc ...

Why isn't setInterval set to a duration of one thousand milliseconds?

While trying to use some code from w3schools, I noticed that the setInterval in the example is set to 5 instead of 5000. Shouldn't it be in milliseconds? If not, how can I speed up this animation? When I try reducing it to decimals like 0.01, the anim ...

Troubleshooting MaterialUI Datatable in Reactjs: How to Fix the Refresh Issue

Currently, I am facing an issue with rendering a DataTable component. The problem is that when I click on the "Users" button, it should display a table of Users, and when I click on the "Devices" button, it should show a table of Devices. But inexplicably, ...