How can I induce the appearance of motion in a stationary div element?

Let's discuss the parallax effect.

Here is a demo I have:

.container {
max-width: 960px;
margin: 0 auto;
} 
div.module:last-child {
margin-bottom: 0;
background-color:#f2f0f2;
}
 div.module.content {
padding: 40px 10px;
height: 15vw;
max-height: 300px;
}
 div.module.parallax {
height: 30vw;
background-position: 50% 50%;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
 div.module.parallax-1 {
  background-image: url("http://safe-castle-3835.herokuapp.com/img/convoy_1920.jpg");
}
 div.module.parallax-2 {
  background-image: url("http://safe-castle-3835.herokuapp.com/img/header.jpg");
}
 div.module.parallax-3 {
  background-image: url("http://safe-castle-3835.herokuapp.com/img/join.jpg");
}
 div.module.parallax-4 {
  background-image: url("http://safe-castle-3835.herokuapp.com/img/join.jpg");
  height: 5vw;
}
<div class="module parallax parallax-1"></div>

<div class="module content">
  <div class="container">

          </div>
</div>

<div class="module parallax parallax-2">
  <div class="container">

  </div>
</div>

<div class="module content">
  <div class="container">

  </div>
</div>

<div class="module parallax parallax-3">
  <div class="container">
    
  </div>
</div>

<div class="module content">
  <div class="container">

  </div>
</div>

<div class="module parallax parallax-4">
  <div class="container">
    
  </div>
</div>

This demo showcases a simple CSS-only parallax effect.

I am looking to enhance this parallax effect by adding movement to the background images. However, since the position of these images is fixed, I am unsure how to create the illusion of movement or change their positions dynamically. I would like to introduce subtle motion to the background images as users scroll through the page, possibly altering their coordinates using JavaScript or CSS transitions.

Answer №1

Ensure that JQuery is being utilized in your code to enable proper functionality.

//Script for Parallax Scrolling

$(document).ready(function(){

  var $window = $(window); 
  var $bgobj = $('div.section.parallax-1'); // defining the object
  var $bgobj2 = $('div.section.parallax-2');

  $(window).scroll(function() {
    var yPos = -($window.scrollTop() / 5);
    // Calculate the final background position  
    var coords = '0% '+ yPos + 'px';
    // Adjust the background placement
    $bgobj.css({ backgroundPosition: coords });
    $bgobj2.css({ backgroundPosition: coords });
  });

});

//End of Parallax Scrolling Script

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

Utilizing Page Methods

I have encountered an issue while using an ajax callback for a void method. When I try to call another method within the calling method, I get an error. Any assistance with resolving this problem would be greatly appreciated. Here is the code snippet: ...

Do I have to include 'document.ready()' for this JavaScript code snippet?

I am currently utilizing freemarker to dynamically generate HTML pages based on user requests. These pages contain a reference to a javascript file in the header section. Within this javascript file, there is an array that is defined. It is necessary for m ...

Enhance your HTML audio player with added timeline functionality

I'm currently working on incorporating an HTML audio player into my project. I've managed to get the play/pause functionality to work, but now I'm stuck on adding a timeline feature. Additionally, I'm not sure how to implement the play/ ...

The CSS layout is not positioning elements correctly

Here is a preview of how I want my final design to appear: I am facing difficulty in properly aligning the two columns using my CSS code. Whenever I apply the float: left; property, the columns end up stacking on top of each other. /*------ Code st ...

Thumbnail vanishes upon being selected

To access my template site currently, please click here. Currently, the first thumbnail on my website is set up to display a gallery of images in FancyBox once clicked, similar to a question asked on StackOverflow that you can view here. Below is a snipp ...

Block elements such as divs are displayed first before inline elements on a webpage

I have multiple child divs inside a parent div. I want the child divs to stack vertically (block) within the height of the parent div, but if they exceed the height, I want them to align horizontally (inline). I have provided an image and included my code ...

Why does JQuery AJAX require a nested DIV to function properly?

Consider the index.php file below : <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body> <form id="MY_FORM" action="./index.php" method="get" ...

Is there a way to undo the CSS rotate animation that has been applied?

Struggling with reversing a CSS animation based on a class being added to a DOM node? Take a look at the code I've been using: EXAMPLE // Closed state @-moz-keyframes spin-close { 100% { -moz-transform: rotate(-0deg); } } @-webkit-keyfra ...

Traverse each child element sequentially using setTimeout or a delay function

Looking to dynamically apply a CSS filter to a list of divs with a delay between each iteration. Here are my attempted solutions: $(this).children().each(function() { $(this).delay(5000).css("-webkit-filter", "brightness(2)"); }); And this alternativ ...

Encountering a 404 error when attempting to use the jQuery .load() function with parameters that include periods ('.')

I am attempting to invoke a controller function using the .load() method, but encountering an error which might be caused by the encoding of '.'. The call I am making is as follows: $("#main-content").load( url + "/" + encodeURIComponent(text ...

The height of a div element does not automatically default to 100% in Next.js

I'm encountering an issue with styling pages in next.js. My goal is to create full-height pages. Although I've successfully set the height attribute in the body and html tags to achieve full height (verified in dev tools), I'm struggling to ...

Avoiding Ajax overload/server collapse in an Arduino/ESP8266 setting

I've recently been delving into Arduino programming to host an HTML/CSS/JavaScript webpage on an Adafruit HUZZAH ESP8266 breakout board. Please pardon any unconventional methods I may be using. My approach involves utilizing Ajax to update pressure g ...

Integration of Tailwind CSS and VueJS single file components within the VS Code environment

Is there a way to configure Tailwind CSS and VS Code in order to prevent errors related to at-rules and empty tag errors inside a VueJS single file component (vue-cli)? <template> ... </template> <style lang="scss"> body { // } ...

When dealing with multiple select fields, the second option will automatically be disabled once the user chooses an item from the list

Is it possible to create a select box where choosing one option will disable the other, and vice versa? Any tips on how this can be achieved? <select> </select> or <select> </select> ...

Toggling display of divs with jQuery using plus and minus icons

Is it possible to use two different icons as a sprite image for when the show and hide functions are active on a div element? For instance, having a + icon displayed when showing the div, and then switching to a - icon when hiding it. The current code I ...

JS filter function is not functioning as expected. It's unclear what the issue might be

Check out my previous inquiry What could be the issue with my filter? I've implemented a filter, but it seems to have some glitches when dealing with specific values. The 'No Record Found' message doesn't appear as expected in certain ...

aligning a form vertically in a container

<div class="parent"> <div class="left-child"> <img src="logo.jpg" alt="logo"> </div> <div class="right-child"> <form action="#"> <input type="text" value="search"> &l ...

What is the process for automatically disabling a checkbox based on the condition that the value in another field is 0?

Before we proceed, feel free to check out the jsFiddle example provided: https://jsfiddle.net/1wL1t21s/ In my scenario, there are multiple checkboxes for selecting toppings with a quantity adjustment feature using the plus (+) and minus (-) buttons. Curre ...

Selecting an option from a dropdown menu and entering data into a text box

I am currently facing an issue with inserting a value using a text box. The process starts with the admin selecting a category, such as a cellphone brand, from a drop down list. Once the brand is chosen, they will input the cellphone model using a text bo ...

What is the rationale behind using :: before display: inline-block in Angular Material MDC's mat-mdc-form-field-error-wrapper, causing substantial padding?

I recently made the switch from Angular Material to Angular Material MDC in preparation for upgrading to Angular 17 from version 15. However, I've noticed that some styles are now broken. One issue I'm facing is a significant padding between the ...