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

Ways to identify an element within a webpage

I'm currently working on creating a chrome extension that can detect the presence of specific elements on a webpage. The goal is to have this extension automatically run every time a new page is opened and display a popup message if the element is fou ...

My React application is not showing the CSS styles as intended

In my project, I have a component file named xyx.js where I properly imported my scss file './xyz.scss'. Both of these files are in the same folder. Interestingly, when I view the styles using the Chrome scss extension, everything seems to be wor ...

The concept of recursion in AngularJS directives

I developed a unique custom directive. angular.module('menu',[]) .directive('MenuDirective',function(){ return{ restrict:'E', replace:'true', scope:{ m ...

Can CSS be utilized to consistently display text in a uniform manner?

Currently developing a unique Qur'an app for Muslims, standing out from the already existing ones in the market. There are two common types of Qur'an apps available: one that replicates the exact look of a physical copy, page by page, using imag ...

Achieve full height without scrolling in React

I am facing an issue where the height:100% property is not working to fill the remaining area on the screen. A red outlined area represents the border radius, while the highlighted yellow space should have been filled: https://i.stack.imgur.com/ZQNkw.png ...

Can this PHP code be optimized for better efficiency?

Seeking to create dynamic links on a page where the link for the current page is excluded. The code functions as desired but seems repetitive - hoping for a more efficient solution, given limited programming experience. <div class= "links"> <?php ...

What options do I have for personalizing this Google Bar Graph?

I am currently working on creating a Google bar chart. You can view my progress here: http://jsfiddle.net/nGvdB/ Although I have carefully reviewed the Google Bar Chart documentation available here, I am struggling to customize the chart to my needs. Spec ...

Error message stating that there is no property 'collection' in Firestore when using Firebase v9 modular syntax in Firebase Firestore

Working on a React application that makes use of Firebase Firestore for handling database operations, I recently upgraded to Firebase version 9 and adopted the modular syntax for importing Firebase services. Nevertheless, when attempting to utilize the co ...

Explore and target elements using browser inspection

Is it possible to create a new CSS class using browser inspection that contains new hover/focus styles? For example: .CanIDo { /*some css rules (ex.)*/ width: 100; } /*my question (USING BROWSER INSPECTOR, not directly typing in the CSS file)*/ .CanI ...

Tips for executing a function on a specific class selector using the document.getElementsByClassName method

When I click on a specific class, I want to implement a fade-out function in JavaScript. However, I am struggling to make a particular class fade out successfully. I attempted to use the this keyword, but it seems like I might be using it incorrectly bec ...

Other browsers, such as Chrome and Firefox, are visible, except for Safari

https://testing007.s3-api.us-geo.objectstorage.softlayer.net/1A23CDC6F75811E6BFD706E21CB7534C---prof__6.jpg This link displays correctly on browsers like Chrome and Firefox, but does not show up on Safari. ...

Color-matching cells must be positioned in the same column within the gridbox consecutively

In my current project, I am dealing with a large number of sections. Each section contains one or two rows with columns ranging from 2 to 15, all of equal width. The cells of the same color are placed in the same column, and the layout looks like this: To ...

Increase the size of the class element when hovered over

I have assigned a class to the "next" and "previous" functions on my webpage, and I am interested in increasing their size on hover so that they take up more space and push the other elements aside. For example: When I hover over the NEXT button, I want i ...

Is it possible to recognize when the mouse button is held down and the cursor is outside the viewport by using mouseleave detection?

Is there a way to detect when a user moves the mouse outside of the view-port, even if they are holding down the mouse button (for example, if the mouse is on the browser address bar)? In the code below, I am currently using mouseout and mouseleave to det ...

Tips for switching a group of buttons within a userscript by clicking a single button?

Apologies if my wording is not clear, allow me to clarify. I am in the process of developing a userscript that will display a set of buttons below a main button when clicked. These additional buttons will serve different functions and should disappear whe ...

Create a dynamic effect by adding space between two texts on the page

const Button = () => { const options = ['test1', 'test2', 'test3']; return ( <div style={{ position: 'absolute', left: '8px', width: 'auto', flexDirection: 'row' ...

How come my footer is appearing at the top of my webpage?

I have encountered a problem where the footer is displaying on top of my page, despite not using any floats, wrappers, or grids in my code. The code snippet can be found below this text. Could someone assist me in identifying why this issue is occurring ...

Utilizing Typescript to pass props to a material-ui button enclosed in styled-components

After setting up my react project using the typescript template, I decided to style the material-ui Button component using the 'styled' method from the styled-components library as shown below: import React from 'react'; import styled f ...

Utilizing R to Extract Data from HTML Webpages

I am currently extracting flight schedules from JFK's website. You can find the link to access the flight schedules right here; My initial approach involves inspecting a specific field of each flight and recording its xpath. The purpose is to first e ...

Showcasing distinct signs in a Span within reactJS is simple by utilizing the appropriate syntax

In ReactJS, I am trying to display the following special characters within a span: <span class='child tm'> <img src={tick} class='tick'/>A special character !"#$%&'()*+,-./:;<>=?@[\]^_`|{}~ </span ...