Guide to creating scrolling parallax web page effects with JavaScript

I am new to Javascript and recently came across a website with a fascinating scroll parallax effect. The images on the site overlap and move as you scroll, similar to the effect on this particular website

Example website:

let scrollH;
let photo1 = document.querySelector(".photo1");
let photo2 = document.querySelector(".photo2");
let photo3 = document.querySelector(".photo3");

document.addEventListener("scroll", function () {
  scrollH = window.pageYOffset;
  if (scrollH > 300) {
    photo1.style.transform = "translateY(-50px)";
  }
  if (scrollH > 300) {
    photo2.style.transform = "translateY(-100px)";
  }
  if (scrollH > 500) {
    photo3.style.transform = "translateY(-190px)";
  }
  console.log(scrollH);
});
body{
   background-color: #AEFEFF;
   height: 2000px;
}

container{
  position:relative;
  margin-top: 500px;
  width: 100%;
  display: flex;
}
.photo1{
  position: absolute;
  top:90px;
  left:20%;
  width: 600px;
  height: 200px;
  background-color: #FFEEAD;
  transform:translate3d(0px, -10, 0px));
  z-index:1;
}
.photo2{
  position:absolute;
  top:30%;
  right:10%;
  width: 800px;
  height: 200px;
  background-color: #D9534F;
  transform:translate3d(0px, 390px, 0px));
  z-index:2;
}

.photo3{
  position:absolute;
  top:50%;
  width: 900px;
  height: 300px;
  border-radius:300px;
  background-color: #96CEB4;
  transform:translate3d(0px, 960px, 0px));
  z-index:3;
}
<div class="container">
  <div class="photo1"></div>
  <div class="photo2"></div>
  <div class="photo3"></div>
</div>

]1

Although I attempted to create a similar effect myself using the simplest method possible, I encountered an issue. While the element does move when the webpage is scrolled, I would like it to return to its original position upon scrolling back up. However, I am unsure how to achieve this effect. If anyone could assist me in modifying this program or offer a better way to achieve the desired effect, I would greatly appreciate it. Thank you.

Answer №1

Here's a suggestion

Assign the class "picture" to all images and include the following code snippet

if(scrolledHeight > 300) $('.photo').css("position", "fixed")

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

Is there a way to apply the 'absolute' or 'fixed' with a width of 100% to the parent div specifically, rather than to the window size?

I would like to create a banner similar to the one shown here: https://i.sstatic.net/SZLr9.png I am attempting to achieve this using 'absolute' or 'fixed' positioning. However, when I tried using 'left: 0 right: 0 top: 0', i ...

Issue with setting .mtl properties in a custom shader in three.js

In my custom three.js application, I am loading an OBJ/MTL model for rendering. I am trying to apply a custom shader to the model, but the color and specular uniforms that I manually pass to the RawShaderMaterial are not updating correctly. Instead, they a ...

What is the proper way to use Object.entries with my specific type?

On my form, I have three fields (sku, sku_variation, name) that I want to use for creating a new product. I thought of converting the parsedData to unknown first, but it seems like a bad practice. export type TProduct = { id: string, sku: number ...

Having difficulty validating the field accurately with Angular.js

In order to validate the input field in accordance with the user's needs using AngularJS, I have shared my code below: <div ng-class="{ 'myError': billdata.longitude.$touched && billdata.longitude.$invalid }"> <input type ...

Using Phonegap alongside ons-scroller and ons-button

Recently, I have been using Phonegap with the Onsen UI system on iOS devices. I encountered an issue where buttons included within an ons-scroller were not clickable when running on an iPad or iPhone. Here is the code snippet that caused the problem: < ...

What is the best way to alter the color of faces in three.js?

I'm currently working with a sample script that involves flying bird objects. However, I am struggling to figure out how to change the color of the birds. I attempted to modify the color on the line where the birds are instantiated: bird = birds[i] ...

Angular - Navigate to Login Page post registration and display a confirmation message

As a newcomer to Angular, I am currently working on an Angular and Spring Boot application. So far, I have created components for user login and registration along with validation features. Now, my goal is to redirect the user to the login page upon succes ...

What are the benefits and drawbacks of combining Jquery UI with AngularJS in web development?

Currently, I am developing a Web application and considering integrating JqueryUI and AngularJS into my ASP.NET MVC project. Is this the best decision for my project? Are there any recommended Databinding libraries that can be used with JQuery UI? Please ...

Click on the links to view various captions for a single image, one at a time

My goal is to create an interactive image similar to what can be found at . (Click Play and navigate to page 5 to see the interactive physical exam). While this example seems to use Flash, I am interested in achieving a similar effect using javascript/jQue ...

Safari on iOS 9 having trouble playing embedded YouTube videos

Recently, I discovered that my YouTube embedded videos are not playing on iOS devices when on my website unless you click in the top left corner of the video. Even after removing all extra YouTube parameters and using standard iFrame embed code, the issue ...

Is there a way to use JavaScript to convert HTML code into plain text?

I am looking to convert an input from an API in the following format: <p>Communication that doesn&#8217;t take a chance doesn&#8217;t stand a chance.</p> into something like this "Communication that doesn’t take a chance do ...

Text with Icon Fonts Does Not Align Vertically

I'm using icon fonts in a nav list and I want to place text between two of the icons. However, there's an issue with alignment - the icons are positioned higher than the text, causing them to not match up well. Is there a solution to this problem ...

Which is more efficient: Loading all data at once as JSON in AngularJS or using a database to load only necessary parts?

I need to showcase products from an ERP system on a website. The ERP system can generate either an XML or JSON file containing all the product information. The website must have features like pagination, sorting, and filtering by attributes. Currently, my ...

Analyzing the audio frequency of a song from an mp3 file with the help of HTML5 web audio API

Currently, I am utilizing the capabilities of the HTML5 web audio API to detect when a song's average sound frequency drops below a specific threshold and create corresponding markers. Although I have successfully implemented this using AudioNodes, th ...

Is ColdFusion AJAX compatible with CF7 or only works on CF9?

Seeking assistance with a simple CFC that functions locally on CF9 but encounters issues on CF7, as it lacks the returnformat attribute in cffunction. How can I resolve this? I attempted to use SerializeJSON() on the returned struct without success. Any su ...

What is preventing me from setting the height of a span to 0 pixels?

It seems that when the height is set to 0px, the element doesn't visually shrink... <div id="bg"> <div id="animate"><span>WINNER ALERT! Click here to get a million dollars!!!</span></div> </div> #bg { back ...

Transform the file format from .casa-model to .obj

Looking for a way to convert a file with the extension .casa-model to .obj format. Is there any solution available? Find the model here. ...

The alignment of the control buttons in Bootstrap 4.4 carousel is incorrect

I followed the official documentation to create a Carousel page. Reference link: https://getbootstrap.com/docs/4.4/components/carousel/#with-controls However, the Carousel Control Button is not in the correct position. img1 img2 It appears that ...

Adjusting the position of the parent div by manipulating the bottom property

Is there a way to toggle the bottom property of a div when clicking its child anchor tag? <div class="nav" :class="{ 'full-width': isFullWidth }"> <a class="toggle-button" @click="toggleNav"><i class="fa fa-angle-down">< ...

Can using display: none impact the SEO of a website's navigation menu?

Is there a difference in SEO impact between using display:none with CSS and using display:none with jQuery for menus? Appreciate the insights! ...