I seem to be experiencing an issue with the script tag not functioning properly in my

I'm facing an issue with the script tag in index.html not functioning properly. I can't figure out why it's not working despite trying to add it into another file and providing a link.

let stars = document.getElementById('stars')
let moon = document.getElementById('moon')
let mountain_behind = document.getElementById('mountain-behind')
let mountain_front = document.getElementById('mountain-front')
let btn = document.getElementById('btn')
let text = document.getElementById('text')
//let header = document.querySelector('header')

window.addEventListener("scroll", function() {
  let value = window.screenY;
  stars.style.left = value * 0.25 + 'px'
  moon.style.top = value * 1.05 + 'px'
  mountain_behind.style.top = value * 0.5 + 'px'
  mountain_front.style.top = value * 0 + 'px'
  text.style.marginRight = value * 4 + 'px'
  text.style.marginTop = value * 1.5 + 'px'
  btn.style.marginTop = value * 1.5 + 'px'
  //header.style.top = value * 0.5 + "px"
})
body, html { height: 1000px; }
<section>
  <img src="img/stars.png" id="stars">
  <img src="img/moon.png" id="moon">
  <img src="img/mountains_behind.png" id="mountain-behind">
  <h2 id="text">Genesis 2k22</h2>
  <a href="#sec" id="btn" class="btn">Register Now</a>
  <img src="img/mountains_front.png" id="mountain-front">
</section>

Answer №1

The root of the problem lies in your method for obtaining the vertical scroll position, which is inaccurate. It would be best to derive this value using the offset and clientY:

let stars = document.getElementById('stars')
let moon = document.getElementById('moon')
let mountain_behind = document.getElementById('mountain-behind')
let mountain_front = document.getElementById('mountain-front')
let btn = document.getElementById('btn')
let text = document.getElementById('text')
//let header = document.querySelector('header')

window.addEventListener("scroll", function() {
  var doc = document.documentElement;
  var value = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  
  stars.style.left = value * 0.25 + 'px'
  moon.style.top = value * 1.05 + 'px'
  mountain_behind.style.top = value * 0.5 + 'px'
  mountain_front.style.top = value * 0 + 'px'
  text.style.marginRight = value * 4 + 'px'
  text.style.marginTop = value * 1.5 + 'px'
  btn.style.marginTop = value * 1.5 + 'px'
  //header.style.top = value * 0.5 + "px"
})
body,
html {
  height: 1000px;
}
<section>
  <img src="img/stars.png" id="stars">
  <img src="img/moon.png" id="moon">
  <img src="img/mountains_behind.png" id="mountain-behind">
  <h2 id="text">Genesis 2k22</h2>
  <a href="#sec" id="btn" class="btn">Register Now</a>
  <img src="img/mountains_front.png" id="mountain-front">
</section>

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

How can I update the version of v8 in NodeJS to ensure I have the latest Javascript enhancements?

Is it possible to upgrade the JavaScript engine version from 3.x to 6.x without updating the Node.js version? The current Node version is 0.12.x. ...

The element does not become properly aligned once it reaches the maximum width

My HTML contains an element with the following style: { max-width: 1500px; padding-right: 40px; padding-left: 40px; width: auto; } Everything looks good when the screen is less than 1580px, and the element is centered. However, when the scree ...

Node.js: Understanding the issue of a mysterious null value in JSON responses sent to clients

As a beginner in the world of Node.js and JavaScript, I have been struggling to find a solution to my problem even after extensive searching. My current challenge involves sending a JSON object to a Node.js server with an array of 2 elements (longitude an ...

Retrieving the contents of an HTML comment tag using Selenium

I’m having trouble retrieving the text inside an HTML comment tag <!-- sample --> that is located within the head section of an HTML page using python 2.7 along with selenium. <head> <!-- I want to extract this statement --> [... ...

What is the solution for fixing a "MongoError: E11000 duplicate key error collection" issue in MongoDB?

After setting up a new Mongo collection for a fresh project, I encountered an error when attempting to add a new user. The error message displayed was as follows: MongoError: E11000 duplicate key error collection: GestorMedico.users index: username_1 dup k ...

Create a responsive DIV element within a website that is not originally designed to be responsive

I am looking to optimize the positioning of an ad div on my non-responsive website. The current setup has the ad displayed at 120x600 pixels, always floating to the right of the screen. While this works well for desktop or large devices, the display become ...

Trigger a click event to alter the child div elements

I have a <div> that expands when clicked and shrinks back when clicked again. Inside this div are images that should only appear once the div expands. My issue is ensuring that all images except the first one are hidden until the div expands. https: ...

Exploring methods to access specific values from an array containing multiple values using Lodash in Angular 4

Hey, I have an array that looks like this: [ 0: "Migration, MD" 1: "Lution, MD" 2: "Mover, MD" 3: "Dee" 4: "Prov10A" ] I would like to extract the values that contain the word "MD" in them. In other words, I want a result like this: [ 0: "Migratio ...

Refreshing the webpage causes a change in the displayed HTML content

Upon the initial load of the HTML, the text will show as "Yes". However, upon reloading or when loading from the cache for the second time, it should display a different text, "NO". ...

Disable the button on smaller devices

I'm trying to make my button take up the full width on smaller screens like mobile devices. Here's what I have so far... Large Device https://i.sstatic.net/TJex1.png Medium Device https://i.sstatic.net/Hvkan.png Small Device https://i.sstatic ...

Struggling to properly position navigation bar items in a Bootstrap website

I'm struggling to align the bar items on a new site that I've built. I've tried placing two bars at the top to mimic the layout of this site: . I'd like it to look similar to this site: , where everything is nicely centered and mobile-f ...

Implementing AJAX to seamlessly add comments in real time!

I have set up a commenting system on my website using two PHP files: index.php and insert.php. The index.php file contains a form where users can input their comments, which are then displayed on the page through an echo statement after being stored in the ...

Obtaining the width of a child table using Jquery

I have a question that might seem simple, but I can't figure it out. How can I use jQuery to get the width of a table that is a child of a div with the class "test"? <div class="test"> <div id="one"> </div> <table> <thead&g ...

css - Arranging pictures in a perfect row

On my webpage, I have 5 images and a div. Currently, the images are displaying one under another, but I want them to appear in a straight line while only showing one image at a time within the div. HTML CODE: <html> <head> <title>My Ci ...

Tips for managing multiple projects in Angular 7 simultaneously

Our team is currently working on an application with two separate workspaces. One workspace serves as the main project while the other is exported as a module to our private npm repository. To access this module, we retrieve it through our package.json f ...

Transforming the unmanaged value state of Select into a controlled one by altering the component

I am currently working on creating an edit form to update data from a database based on its ID. Here is the code snippet I have been using: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material ...

Tips for resolving conflicts with jQuery on an ASP.NET webpage

On this page, I am using references and scripts for a date time picker and color box. However, only one of them works at a time - commenting out one script allows the other to work. I have tried using jQuery noconflict, but it did not resolve the issue. H ...

Is it necessary to include the Fonts file (which accompanies Bootstrap files) in the HTML document?

As a beginner in HTML, I decided to incorporate Bootstrap into my project. After downloading the necessary files, including css, js, and fonts, I found myself able to easily add the css and js files using the appropriate tags. However, I encountered some c ...

Adding JSON data to an HTML attribute: A step-by-step guide

I need help with appending one of the values from my JSON object to an HTML attribute value. This is what I'm currently trying: <div ng-messages="registrationForm.[contact.id].$error"> In this example, contact.id is coming from a JSON object, ...

Creating a hexagon pattern within an array

I'm in need of assistance in writing a function that can generate a hexagon pattern within a 2D array. I only have the column size of the array, and the rest of the dimensions must be calculated. https://i.sstatic.net/WHuYF.png Unfortunately, I&apos ...