Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Javascript for fading an image on-scroll is still showing the image on page load

I've got an image in a fixed-position div. The following code is fading the image in where I want to, but initially the image is visible. Once I start scrolling, it disappears and then reappears when it should. Trying to figure out why the image is displaying on page load.

    const checkpoint = 500;

    window.addEventListener("scroll", () => {
      const currentScroll = window.pageYOffset;
      if (currentScroll <= checkpoint) {
        opacity = 0 - currentScroll / checkpoint;
      } else {
        opacity = 1;
      }
      document.querySelector(".sticky-logo-fade-in").style.opacity = opacity;
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can store the callback in a variable, then call it on page load:

const checkpoint = 500;

const scrollHandler = () => {
    const currentScroll = window.pageYOffset;
    if (currentScroll <= checkpoint) {
        opacity = 0 - currentScroll / checkpoint;
    } else {
        opacity = 1;
    }
    document.querySelector(".sticky-logo-fade-in").style.opacity = opacity;
}

window.addEventListener("scroll", scrollHandler);

scrollHandler();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!