Adjust div position as the page is being scrolled

Although similar questions have been posed before, this one is different!

There are three divs present on my webpage.

WRAP serves as the main page wrapper with a border surrounding the inner divs.

OUTER represents the outer div.

RIGHT is the specific div that I want to move along with the page scroll.

You can observe in the image where RIGHT is positioned, and as the page scrolls down, I aim for RIGHT to follow.

I attempted setting the CSS position to fixed; however, resizing the page disrupts the layout. The current CSS settings are as follows:

#wrap 
{ 
    width: 100%; 
    margin: 50px auto; 
    padding: 20px 20px 20px 20px; 
    border:2px solid #800000; 
}

#outer 
{
    margin: auto;
    width: 700px;
    height: 1250px;
    display: table;
    border: 2px solid #000008;
}

#right 
{
    float: right;
    width: 100px;
    height: 100px;
    border: 2px solid #008000;
}

To visualize the current appearance, I've crafted a demo.

Any suggestions on how to smoothly transition RIGHT with the scrolling of the page?

Many thanks

Answer №1

If you're looking for a solution that ensures the div doesn't overlap with the content on smaller windows, RouthMedia's answer might work for you.

An alternative approach is to implement a window.onresize function that dynamically adjusts the right position of the div based on your specific constraints:

window.onresize = function(event) 
{
    // Calculate new position if window size meets certain criteria
    ("#right").css("right", newpos);
};

Update: It appears you prefer keeping the div inside the outer container.

One method is to initialize the "right" property within the inner div when the page loads, ensuring it stays contained as the screen resizes.

Another option is to utilize position: absolute and update the "top" property using document.scrollTop during the onscroll event trigger.

Answer №2

To achieve a fixed position on the right side of your webpage, include the CSS rule position:fixed; right:0px; for the #right element.

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

Send a file using an HTTP POST request

I am faced with a challenge involving two <input> tags on my client that allow the user to choose a file. Once the files are selected (24-bit BMP 640*480), I need to send an HTTP POST request in order to save the imageData of each file and retrieve i ...

I'm having trouble with this jQuery script - it doesn't seem to be functioning properly. I can't seem to spot any

Just getting my feet wet with jQuery. I'm trying to make a simple button fade slightly, but it's not working for some reason. My goal is to have the button fade in slowly. $(document).ready(function(){ $('button').mouseenter({ $(&ap ...

Alert by BootStrap

I created a JSFIDDLE sample showcasing an alert that fades out upon clicking a button. I found a code snippet that accomplishes this, but now I want to ensure that the div containing the button remains unaffected both before and after the click event. By u ...

Struggling to get the onHover effect working with the Grid item component in MaterialUI

I'm currently working on a Grid layout with nested Grid components, but I've run into an issue where applying inline styles to the Grid item component isn't working as expected. As a newcomer to MaterialUI in ReactJS, I could really use some ...

Designing fixed sliding icons on the left side with CSS

I'm looking to replicate a sticky sliding icon set on both the left and right using CSS. While the right side icons with text are working perfectly, the ones on the left side are not functioning as expected. Here's the code snippet: .sticky-con ...

Utilize AJAX in JavaScript file

I am encountering an issue with the following code: function inicioConsultar(){ $(function(){ $('#serviciosU').change(function(){ if ($('#serviciosU').val()!= "-1") { $.ajax({ url: "@Url. ...

Is there a way for my element to fill the entire screen and prevent any scrolling from occurring?

I am looking to ensure that my children's div occupies the remaining space on the screen and prevent scrolling. Since I am utilizing PanZoom, I need the children's div to always fill 100% of the screen. <div class="parent"> &l ...

Maintain the link's functionality even after it's been clicked by utilizing the same

I have a fixed navbar with same-page anchors and I want to keep the link active after it has been clicked and taken to that section of the page. While this is simple for links to another page, I'm struggling to figure out how to do it here. Here&apos ...

Tips for positioning elements in a way that prevents CSS fixed from overlapping upon page loading

Is there a way to adjust my navigation menu to prevent overlap upon page load, but have it overlap when scrolling down (Dynamic nav bar)? Check out my current setup: Upon initial page load, the navigation bar overlaps the text. I would like the navigatio ...

Assistance with Blackberry Webworks (Widget) JavaScript & XML/JSON Integration

Let me paint you a picture... I am in the process of crafting a Blackberry WebWorks (widget) that serves as a gateway to an external website. The majority of the pages reside on this remote site, with the exception of a user customizable homepage where us ...

Add a circular transition effect to Font Awesome icons

Check out my code snippet on JSFIDDLE: https://jsfiddle.net/8f3vLh0a/14/ I am using font awesome icons and I would like to add a circling effect on hover similar to this demo: http://tympanus.net/Tutorials/ResponsiveRetinaReadyMenu/ How can I implement t ...

Is the 'name' field empty in PHP contact form emails being sent to the email address?

Sorry, I'm new to filling out this type of form! I managed to create a contact form page that sends me the 'email address' and 'message' from the form fields, but it's not sending the value entered in the name field, it remai ...

Adjusting size of a div as you scroll - Java Script

Using the codepen provided at http://codepen.io/anon/pen/fDqdJ, I am looking to enhance the functionality by implementing a feature where, upon reaching a specific distance from the top of the page, an already animated div undergoes a change in scale while ...

Utilizing Jquery to transform characters into visual representations

Do you think it is wise to utilize a jQuery function that replaces each character in a text with custom character images? For instance, when using the function, you need to specify which element you would like the characters replaced in, and jQuery will d ...

Position an HTML canvas at the very top of the webpage

Can someone help me figure out how to align a canvas element to the very top (0, 0) of a webpage? I attempted using margin: 0; padding: 0px;, but it didn't work. There always seems to be some white space at the top that I can't eliminate. ...

Navigating sidebars with jQuery Dropdowns

Currently, I am working on creating a slide-in and out navigation menu that displays icons when minimized. Upon hovering over the icons, the menu expands to reveal links. However, I am encountering an issue where I cannot get a dropdown to display when hov ...

Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code: $('#PlatformErrorsTableData').html(table1Html); $('#PlatformErrorsTableData div:nth-child(even)').css(" ...

The issue of centering not working with a specified width under the CSS rule `margin: 0 auto

As I was creating sections for a mini "about me" page, I encountered an issue with setting margins in all divs (this one is the most important). Despite specifying widths and heights, I couldn't get margin: 0 auto; to work. body { background: #aaa; ...

Adjusting text sizes using HTML and JavaScript

I stumbled upon some code that allows website visitors to change font size at this link. I modified the code to make use of buttons for this functionality. <html> <head> <script language="JavaScript" type="text/javascript"> function cha ...

Ways to retrieve HTML tags from a website's DOM and shadowDOM

I am currently working on a project using NodeJS where I need to extract the HTML structure of multiple websites. However, I am facing some challenges with this task. My goal is to retrieve only the HTML structure of the document without any content. It is ...