Enable scrolling for a div positioned beneath another div

I am working with an iPad frame and trying to implement a feature where a larger image scrolls behind it as the user scrolls down the page. Although my CSS is more complex than the example provided in this link, I am struggling to get even that basic functionality to work.

On my actual webpage, I want the scrolling behavior to be normal until the user reaches the large image behind the iPad frame. At that point, I'd like the scroll action to affect the "page content" within the image. Once the content of the image ends, I want users to continue scrolling normally.

After updating the fiddle to better reflect what I'm aiming for, I realize that when I position the iPad frame on top of the image, the content doesn't scroll properly. However, placing the frame beneath the image causes issues with resizing and covering other elements on the page. Any suggestions on how to achieve this effect seamlessly? Thank you to Felk for pointing me in the right direction.

Included below is a visual representation of how I intend to apply this concept:

Example HTML:

<div class="container">
    <img class="frame" src="http://s11.postimg.org/44ejhu0jn/ipad_frame_780.png" />
    <div class="inner">
        <img src="http://s11.postimg.org/xtwbnx937/ipad_content_660.png" />
    </div>
</div>

Example CSS:

.container {
    width: 70%;
    position: relative;
}
.frame {
    /* position: absolute; */
    width: 100%;
    z-index: 100;
}
 .inner {
    height: 558px;
    overflow: scroll;
    position: absolute; 
    top: 14%;
    left: 38px;
}
.inner img {
    width: 92%;
    z-index: -100;
}

Answer №1

Alright, so I attempted to repair your fiddle, but in the process, I ended up making too many changes.

Let me walk you through what I would do if I were to tackle your project (assuming I correctly grasped your query).

Firstly, I would position the iPad image in the background using position:fixed and a negative z-index. This way, the image remains stationary as its position is relative to the window rather than any specific element. Additionally, the initial part of your content will overlay the image and scroll smoothly.

Next, we need to ensure the correct flow of HTML elements when scrolling, with more content appearing beneath the first one (later underneath the image). To better illustrate this issue, I added another div with a red background.

The HTML structure would resemble something like this:

<div class="container">
    <div class="outer">
        <img class="" src="http://s11.postimg.org/xtwbnx937/ipad_content_660.png"/>
    </div>
        <div class="frame">
    <img class="ipad" src="http://s11.postimg.org/44ejhu0jn/ipad_frame_780.png" />
        </div>

    <div class="moreContent"></div>

</div>

Now we focus on separating the top content from the bottom content. By adding a significant margin-bottom to the first content, when scrolling down, once you reach the end of the first content, the background image becomes visible. Following that, after exceeding the margin, the last content flows above the image, which is not the desired effect.

To summarize, here's the current progress: FIDDLE1

Now, a straightforward jQuery solution is needed (always simple when feasible). A few directives instruct the browser, as shown below:

$(window).scroll(function () {
            if ($(window).scrollTop() > 1127) {
                $(".frame").addClass('relative');
                $(".outer").addClass('no-margin');
            } 
             else {
                $(".frame").removeClass('relative');
                $(".outer").removeClass('no-margin');
            }           
        });

In essence, these commands specify that upon surpassing 1227px in height while scrolling, classes should be added to frame and outer, only to be removed when scrolling back. The class applied to outer eliminates the vast margin between the first and last divs, while the class given to frame ensures the image container retains normal positioning as the rest of the elements scroll downward.

The values I used, particularly 1227px, are based on the JSFiddle images provided, but in future projects, determining the actual height of your initial content can be easily done by inspecting it with Chrome or a similar tool. Similarly, adjusting the considerable margin I included is also possible.

The remaining adjustments were made to correct sizes and center all elements within the window at a width of 600px.

For the final version, check out this link: FIDDLE

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

Ajax function implemented successfully with stylish design

Hi there! :) I'm encountering an issue with my code. I am trying to create a dropdown filter using ajax, php, and json. The first dropdown menu (for selecting the build year of a car) is populated through php. The second dropdown menu allows users to ...

"Enhance Your Design: Creative CSS Animation Effects for Underlined

I've been experimenting with incorporating animation into material-UI, specifically making the underline move from left to right for a tab. Here's the code I've come up with: const useStyles = makeStyles({ link: { padding: "1rem&quo ...

Enforcing automatic spell check on dynamically generated content

After some experimentation, I have discovered that the spell check feature in most browsers is only activated when the user moves to the next word after inputting text. Another interesting finding is that it is possible to "query" the spellchecker by simpl ...

Is there a way to have one element automatically expand when another is selected?

I'm currently utilizing the date and time picker from . However, I need some assistance with the current setup. Currently, the date and time pickers are in separate input fields. In order to select a date, I have to click on the Date input field, and ...

What are the steps to clipping a canvas using CSS clip-path?

When it comes to clipping a canvas, there are multiple methods that can be used. One way is to create a path with getContext('2d') and set globalCompositeOperation. Another method involves using -webkit-clip-path or clip-path, though the latter m ...

Why is it that every time I write this code, the localhost gets redirected and a message pops up saying

<?php include("connect.php"); if(isset($_POST['submit'])) { $qonax=@$_POST['qonax']; $subject=@$_POST['subject']; $insert="INSERT INTO subject(ID,Qonax,Subject)VALUES('','$qonax', ...

Using ASP.NET Server Controls to Toggle Textbox Visibility Based on Dropdown Selection

What is the optimal method for displaying or hiding a textbox or an entire div section based on a user's selection from a dropdown menu? I am uncertain if this can be achieved with server controls, so would it require using standard client-side HTML c ...

What is the proper method for filling a custom shape that has been created in Canvas?

I am attempting to create a custom shape, but I am facing an issue where using moveTo prevents me from filling it. Is there any method to determine points on the screen to fill a shape? Or should I use or draw another real shape in the same layer block? T ...

Mixing Module Federation with different React versions and individual CSS isolation

In my setup, the module federation remote repository is: developed using react 17 utilizing material-ui 4 with jss incorporating global CSS from third-party libraries that cannot be modified Meanwhile, I have several hosts that consist of: different ver ...

Modify picture properties when listbox is altered using jQuery

I need to set up a unique album gallery display where different folders are selected based on the item chosen in a selectbox. Below is the HTML code: <select name="album" id="album" onChange="changeimage();"> <option value="0" selected disab ...

Most effective method for displaying 100 images on a single page?

I have a webpage on my site that displays 100 images, but loading them one at a time makes it look unattractive. Is there a way to make it more elegant and visually appealing? ...

Issues of horizontal spaces in HTML emails

We have been facing an ongoing issue with the layout of our email communications. Upon rendering in Outlook, the emails are displaying additional horizontal gaps/spaces. However, the web version of the email appears to be fine. It's unclear to us the ...

Tips on making a progress bar with a reverse text color for the "completed" section

I've been tackling this issue for hours, but haven't found a working solution yet. The challenge is to create a progress bar with a dynamic width and a centered label that changes its text color between the progressed and unprogressed parts. Here ...

HTML - Align 3 tables horizontally with text below

I have successfully floated 3 tables next to each other using <table style="float:left;">. However, I am facing an issue where some text is being wrapped to the right side of the right-most table instead of being left-justified at the very ...

Errors from jQuery validation are causing disruptions in my form's functionality

When jQuery validation error messages appear, they take up space and push other fields downwards. I want the error messages to adjust within the available space and not disrupt other fields or elements. Below is the jQuery validation code I'm currentl ...

The renderToString function in Material UI's sx property doesn't seem to have

Every time I apply sx to a component that is rendered as a string and then displayed using dangerouslySetInnerHtml, the styles within the sx prop do not work. Here is an example showcasing the issue: Codesandbox: https://codesandbox.io/p/sandbox/wonderfu ...

Challenges faced when using jQuery UI autocomplete

My code utilizes jQuery UI's autocomplete method on a text box: $(function() { $('#project').autocomplete({ source: function(request, response){response(Object.getOwnPropertyNames(projects))}, minLength: 0, ...

Server time dictates the operation of Moment.js

I've been working with Moment.js and it's functioning correctly, but I can't seem to figure out how to make it run on server time instead of local time. I've tried a few things, but haven't had any luck. I'm unsure of how to ...

Click to position custom text on image

Is there a way to adjust the position of text on an image after it has been clicked? I have included an image below to demonstrate how I would like it to appear: https://i.stack.imgur.com/unmoD.png function revealContent(obj) { var hiddenText = obj. ...

The stop-color property is not functioning as expected in CSS/SVG

Here is the code I am currently working with: <?xml version="1.0" standalone="yes"?> <svg xmlns="http://www.w3.org/2000/svg" width="360" height="240"> <style>stop{stop-opacity:1}circle{fill:url(#r)}</style> <defs> <radialG ...