Inconsistent movements of parallax

My website is designed to have smooth parallax scrolling on every page except the home page.

Despite using the same code, I can't figure out why there is such a difference in behavior.

I've tried everything possible but still can't determine the cause of this issue.

Necessary code provided below. Live preview:

CSS

.homeIllustration {
    width: 100%;
    height: auto;
    position: absolute;
    transition: none;
}
.rightSide {
    display: inline-block;
    position: absolute;
    width: 70%;
    margin-left: 70px;
}

Javascript

if ($(window).width() > 800){
   window.addEventListener('scroll', () => {
   let parent = document.getElementById('illustrationContainer');
   let children = parent.getElementsByTagName('img');
   // document.getElementById("illustrationContainer").style.transform = 'translateY('+ window.pageYOffset* 1 + 'px)';
   for(let i = 0; i < children.length; i++) {
     // children[i].style.transform = 'translateY(' + (window.pageYOffset / (2 + 0.3*i) * children.length / 32) + 'px)';
    children[i].style.transform = 'translateY(' + (window.pageYOffset - ((window.pageYOffset / i)/5)) + 'px)';
   }
}, false);
}

Answer №1

If you're hesitant about using this code, consider incorporating CSS transition for a smoother effect. Otherwise, using a JavaScript library can also achieve the desired outcome.

window.addEventListener('scroll', () => {
     const distance = window.pageYOffset;
   let parent = document.getElementById('illustrationContainer');
   let children = parent.getElementsByTagName('img');
   for(let i = 0; i < children.length; i++) {
     children[i].style.transform = `translateY(${distance * 1}px)`;
   }
}, false);
.homeIllustration {
    width: 100%;
    height: auto;
    position: absolute;
    transition: transform .1s linear;
}
.rightSide {
    display: inline-block;
    position: absolute;
    width: 70%;
    margin-left: 70px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="illustrationContainer">
<img class="homeIllustration" id="homeIllustration5" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Planet.svg" style="opacity: 0;">
                    <img class="homeIllustration" id="homeIllustration5" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Planet.svg" style="transform: translateY(0px);">
                    <img class="homeIllustration" id="homeIllustration1" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Square.svg" style="transform: translateY(0px);">
                    <img class="homeIllustration" id="homeIllustration2" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Mountains.svg" style="transform: translateY(0px);">
                    <img class="homeIllustration" id="homeIllustration3" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Man.svg" style="transform: translateY(0px);">
                    <img class="homeIllustration" id="homeIllustration4" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Splashes.svg" style="transform: translateY(0px);">
                    <img class="homeIllustration" id="homeIllustration5" src="https://watwiljij.nu/wp-content/themes/generatepress/assets/WWJ_Home_Illustration_Plane.svg" style="transform: translateY(0px);">
                </div>
  Lorem ipsum dolor, sit amet consectetur adipisicing elit...

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

Changes in React BrowserRouter URLs are not reflected on the page or components, requiring a manual refresh for them to

Greetings, fellow developers! I have developed an app using React with a remote menu component. Despite trying numerous techniques, I am facing an issue where my URL changes but the components are not rendering on the screen. You can check out the code h ...

Expanded MUI collapsible table squeezed into a single cell

I'm experimenting with using the MUI table along with Collapse to expand and collapse rows. However, I've noticed that when utilizing collapse, the expanded rows get squished into one cell. How can I adjust the alignment of the cells within the p ...

I'm feeling lost trying to figure out how to recycle this JavaScript function

Having an issue with a function that registers buttons above a textarea to insert bbcode. It works well when there's only one editor on a page, but problems arise with multiple editors. Visit this example for reference: http://codepen.io/anon/pen/JWO ...

Exploring a previously created Vue mini project that is experiencing technical issues

Hello everyone! I am revisiting a project that I haven't worked on in a while. I tried using Vue.js for the first time to create an archery calculator for kinetic energy, velocity, and more. Instead of setting up a full Vue app, I simply used a CDN l ...

The Facebook app is designed to require users to grant permission for the application to access their personal data

I am developing an application that replicates Facebook's functionality. When a user purchases my application, they are granting permission for my application to access their data on Facebook. Is there a way to programmatically click on the "Allow App ...

showing text style that is only specified on the server, not by the client

I am in the process of creating a new website that offers SMS services through clickatell. This website includes various font families that may not be defined on the client's computer. For instance, if the site is accessed from a different computer, t ...

Unexpected issues arose with the seemingly straightforward Angular/Node application

Recently, I developed a basic application using Angular and Node which serves as a chat platform. One interesting feature is that it utilizes ng-model on a text field. However, I encountered an issue where the message being emitted by the node was consist ...

Loading web pages using ajax and handling relative paths

My method involves using ajax to load HTML files when links on my page are clicked. The process is simplified as follows: links.click(function () { var href = $(this).attr("href"); history.pushState(null, null, href); $.get($(this).attr("href" ...

"Encountering an issue when trying to save thumbnailPhoto with NodeJS and ldapsj

Utilizing the ldapsj-client module, my goal is to store the thumbnailPhoto data into a file. const auth = async () => { const client = new LdapClient({ url: 'myaddomain' }) await client.bind('<a href="/cdn-cgi/l/email-protect ...

Transform the JavaScript function to a Node.js module

My function serves as an abstract factory for creating JavaScript objects. Here is the code: var $class = function(definition) { var constructor = definition.constructor; var parent = definition.Extends; if (parent) { var F = function ...

Issues with JavaScript Array Splice Function

var cache = []; cache[0] = "0"; cache[1] = "1"; cache[2] = "2"; cache[3] = "3"; cache[4] = "4"; cache["r"] = "r"; console.log(cache.length); for(key in cache){ if(isNaN(key))continue; else cache.splice(key,1); // The functionality of cache.splice(k ...

Unchanging Arrays within JavaScript

Is there a way to maintain a set of unchanging values in JavaScript effortlessly? Furthermore, how can I ensure that comparisons between these constants yield accurate results? For example: const data = [5, 10, 15]; data[1] = 20; // Modifying an element ...

Steps for eliminating the thick border around <thead> and <tfoot> elements in Bootstrap

Is there a way to remove the thick border on the <thead> and <tfoot> elements in Bootstrap 4 that seems a bit unnecessary? I found a forum post addressing this issue for Bootstrap 5, but unfortunately, the fix didn't work for me since I a ...

Swapping out a code snippet within an HTML document using JavaScript

As a new member of this community, I present to you my very first problem that needs solving. It might be a piece of cake for some of you, but for me, it's proving to be quite tricky. The task at hand involves replacing or removing a string to make ev ...

Addressing an error of "call stack full" in nextjs

I am currently working on a project in nextjs where I need my billboard to continuously scroll to show different information. The Nextjs debugger keeps showing me an error message that says 'call stack full'. How can I resolve this issue? con ...

Is there a way to make a div rotate from one of its ends rather than its center with atan2() method?

I want the div.stick in the following code to rotate based on a different origin point. Currently, it rotates from the center but I would like it to rotate from the bottom of the stick instead. The rotation should occur when the mouse is aligned with the t ...

CSS Code That Is Ineffective

Having some trouble with creating a hidden background that will still show up but not on the main content page. I'm looking to have a different background for the main content itself, but for some reason it's just not working. The main background ...

Creating a tree-like design with the spry accordion換stepping up your spr

I want to style my accordion in a way that it resembles a tree structure. How can I achieve this using CSS? For example: + should be displayed when the accordion tab is closed, and - when the accordion is open. <div class="Accordion" id="systemAccordi ...

Converting milliseconds into a date format

I am looking to change milliseconds into a string that displays the time duration. function convertMsToTime(ms) { return ('' + Math.floor(ms / 86400000) + 'd -' + Math.floor((ms % 10) / 60).toString().padStart(2, '0')); } ...

What is the best way to demonstrate the advantages of relative division compared to absolute division

There are two sections labeled as a and b. .a { height: 120px; width: 120px; background: #E08027; border-radius: 50% 50% 10px 10px; position: relative; } .b { position: absolute; height: 49px; width: 49px; background: #F2AD43; borde ...