Update the stationary background image on select div elements

Currently working on a "our story" section where the divs move up and change the background image when they reach the center of the screen. I am aiming for a fixed background image that transitions smoothly when the div is in the middle. The relationship between the background image and the div is crucial.

Still honing my styling skills as I strive to achieve the desired effect.

For reference, you can view what I'm trying to accomplish at the following link:

Reference:

.parent {
  background-image: url(https://picsum.photos/1080/1920);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  height: 300vh;
  text-align: center;
  color: white;
}

.div1{
  background-color: aqua;
  height: 50vh;
  margin-top: 90vh;
  padding: 20px;
  width: 400px;
  margin: 90px auto

}

.div2{
  background-color: skyblue;
  height: 50vh;
  padding: 20px;
  width: 400px;
  margin: 90px auto
}

.div3{
  background-color: grey;
  height: 50vh;
  margin-top: 90vh;
  padding: 20px;
  width: 400px;
  margin: 90px auto

}
<div class="parent">
<div class="div1" >
Content about desert
</div>

<div class="div2" >
Content about mountains
</div>

<div class="div3" >
Content about birds
</div>
</div>

Answer №1

If you're looking for a starting point on how to achieve this, the code snippet below could be helpful. It's a basic example to get you started and provide some direction for further exploration.

var sections;
const backgrounds = [
"url(https://i.picsum.photos/id/604/1080/1920.jpg?hmac=ZGdArZ8jNmfX3SDYlQMsYeGsKy6b_1uBH2WGFt3wYzk)",
"url(https://i.picsum.photos/id/301/1080/1920.jpg?hmac=7cyfS0iaSux6TVm9Wnkg4vtXIWyg_YyhhWYHM6yUTnE)",
"url(https://i.picsum.photos/id/155/1080/1920.jpg?hmac=UZT1atpRomcjj2ijIjwR9C3DY0xtD4On3m5XMNLak6o)"
];
var parent;
var bottomOffset;
function getViewScrollingElements() {
    parent = document.getElementById("parent");
    sections = document.getElementsByClassName("section");
    bottomOffset = window.getComputedStyle(parent).getPropertyValue("height");
    bottomOffset = parseInt(bottomOffset.substr(0, bottomOffset.length - 2)) / 3;
    //  Do first check before a scroll could have occurred
    checkInView();
}
function checkInView () {
    var boundingRect;
    for ( var i = 0; i < sections.length; i++ ) {
        //  Get the extremities of the div
        boundingRect = sections[i].getBoundingClientRect();
        //  Are the div's extremities in view
        if ( boundingRect.top < window.innerHeight && boundingRect.bottom >= bottomOffset ) {
            parent.style.backgroundImage = backgrounds[i];
            break;  //  Only want one at a time
        }
    }
}
.background {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
}

.parent {
  text-align: center;
  color: white;
  height: 100%;
  overflow-y: auto;
}

.section {
  height: 70vh;
  padding: 20px;
  width: 400px;
  margin: 90px auto;
  opacity: 85%;
}

.section:nth-of-type(1) {
  background-color: aqua;
}

.section:nth-of-type(2) {
  background-color: skyblue;
}

.section:nth-of-type(3) {
  background-color: grey;
}
<body onLoad="getViewScrollingElements()">
<div class="background"></div>
<div id="parent" class="parent" onScroll="checkInView()">
    <div class="section">
        Content about desert
    </div>
    <div class="section">
        Content about mountains
    </div>
    <div class="section">
        Content about birds
    </div>
</div>
</body>

Please note that this code may not work properly in SOs fiddler due to iframe restrictions, but it should function as intended in Chrome and Firefox browsers.

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

Tips for dynamically resizing a div element as a user scrolls, allowing it to expand and contract based on

I was working on a project and everything seemed easy until I hit a roadblock. What I am trying to achieve is expanding a div to 100% when scrolling down to the bottom of the div, then shrink it back to 90% at the bottom and do the reverse when scrolling ...

Retrieving XML data from an external API is essential for integrating information

For a personal project, I am working on a mashup and trying to integrate a webservice that I came across. You can find the webservice here: Whenever I attempt to call it via ajax (using FireFox 7 in this case), I constantly encounter the following error ...

Encountering a 500 Internal Server Error while attempting to insert data into a MYSQL database using

I am experiencing an issue while trying to insert radio checked data values into a MySQL database using PHP, jQuery, and AJAX. Whenever I attempt to insert the values, I encounter an internal server 500 error... Any assistance would be greatly appreciated ...

How can we show a React JS component when clicked, without using the App.js file for display?

How can I display a component onclick in React JS without using UseState in a file other than App.js? This code will be in App.js var [clicks, setClicks] = useState(false) return( <> {clicks && &l ...

Adjusting the maximum character limit of the text input field

After receiving some values from C# via AJAX, I need to bind them to a GridView. Everything is working fine, but the only difference is that instead of binding the value to a textbox's property, specifically the MaxLenght property, it needs to be boun ...

How to trigger a forced download of a .csv file instead of displaying it in the browser using HTML

After clicking the download button, it should redirect to a new "thank you" webpage and then automatically start downloading a .csv file from the SD card after 2 seconds. However, instead of downloading the file, it is being displayed in the browser. Belo ...

Ways to align an inner circle in the middle using CSS

div.container{ border:2px dashed blue; position:relative; } span.parent{ display:inline-block; width:40px; height:40px; border:2px solid black; border-radius:50%; text-align: center; position:absolute; right:20px; top:10px; } span.ch ...

Enhancing User Interfaces with JQuery UI Widgets and Dynamic AJAX Calls

Currently involved in geolocation and mapping work, I am creating a JQuery widget to ensure that the code is portable for future projects. However, I have hit a roadblock when it comes to making an AJAX request. Below are a couple of methods from my widge ...

Enhancing user experience with autocomplete functionality using jQuery combined with server

Struggling with autocompleting a search field using jQuery-UI and encountering issues when trying to integrate PHP as the data source. The setup works smoothly with a variable as the source. JS: $(function () { var data = [ "ActionScript", ...

Having trouble adding your own styling to bootstrap-4 using a custom CSS file?

Here is the code I used to connect my two stylesheets, with the custom css file being linked second as per what I believe is the correct way to do it. <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href=" ...

I have developed a website that can calculate the factorial of any given number. The next step is to design a function that will display the step-by-step mathematical process

Could use a hand with this one. The function below is empty and I'm a bit stuck on what to do next. Right now, the webpage displays the factorized number, but I want to show the mathematical equation before it, like so: User inputs: 3 Site outputs: " ...

Struggling to get .parent().toggleClass to function properly

Check out this fiddle: https://jsfiddle.net/qxnk05ua/2/ I'm trying to get the background color to change when I click the button, but it's not working. Can you help me figure out why? This is the Javascript code I'm using: $(document).rea ...

Adjusting the height of content in Angular Material 2 md-tab

I've recently started working with angular material and I'm facing an issue while trying to implement md-tab into my application. The problem is that I can't seem to style my tab content to take up the remaining height of the screen. Could s ...

organizing data input into a POST array

I have developed a form that contains multiple identical sections. These sections can be dynamically added using jQuery. Each section represents one workstep involved in assembling a product, and therefore includes the same input fields (description, durat ...

Adjust the height of a div to match the tallest element in the same row using jQuery and Javascript

I need to display multiple rows of products, each row containing 4 products. The code snippet below shows an example with only 1 product. To create a row with 4 products, the code for <div class='col-xs-6 col-sm-3 col-md-3'> needs to be rep ...

How to share custom data with fb_share feature while displaying box count? (find out more below)

I am trying to implement Facebook sharing with box count on my website. The code I am using is as follows: <div id="fbShare"> <a name="fb_share" type="button_count" expr:share_url="data:post.url" href="http://www.facebook.com/sharer.php">S ...

How to disable the automatic sliding of the Twitter Bootstrap carousel when the page first loads

Is there a way to stop the Twitter Bootstrap carousel from automatically sliding when the page loads, and only start sliding when the next or previous button is clicked? Thank you ...

Connect to a particular section of the webpage with additional top spacing

The navigation on this page is not transparent. Whenever I link to a specific part of the page, some text becomes hidden behind the navigation bar. This is illustrated in the image below: https://i.sstatic.net/h0ExX.png What I desire is a navigation bar l ...

Creating a custom Jquery function to generate a Div element instead of a Textbox in your web application

I need assistance with a jquery function that retrieves data from a JSON PHP MySQL setup. The retrieved results are currently displayed in textboxes. function showData(wine) { $('#Id').val(wine.id); $('#question').val(wine.question ...

Selenium VBA - Issue with checking checkbox

I'm currently working with a spreadsheet that is extracting data from a website. One aspect of my project involves checking a tick box. Although I can easily locate the element using FindElementbyCSS, clicking it doesn't actually select the tick ...