skip to Main Content

I am trying to change the background color of body on scroll, but it seems like the scroll event listener isn’t working. here is my code:

"use strict";

// Get all the sections with the class "section"
const sections = document.querySelectorAll(".section");

// Function to update the background color based on the section in view
const updateBackgroundColor = () => {
  console.log("updateBackgroundColor called");

  const scrollPosition = window.scrollY || window.pageYOffset;
  console.log("scrollPosition:", scrollPosition);

  sections.forEach((section) => {
    const sectionTop = section.getBoundingClientRect().top + scrollPosition;
    const sectionBottom = sectionTop + section.offsetHeight;
    const backgroundColor = section.getAttribute("data-bg-color");

    console.log(
      `Section: ${section.classList}, Top: ${sectionTop}, Bottom: ${sectionBottom}, Background Color: ${backgroundColor}`
    );

    if (scrollPosition >= sectionTop && scrollPosition <= sectionBottom) {
      // The section is in view
      if (backgroundColor) {
        document.body.style.backgroundColor = backgroundColor;
      }
    }
  });
};

// Get the container element
const scroller = document.querySelector(".scroller");

// Add an event listener for the "wheel" event on the container
scroller.addEventListener("wheel", (event) => {
  // Prevent the default scroll behavior
  event.preventDefault();

  // Calculate the scroll direction
  const delta = event.deltaY;

  // Scroll to the next or previous section based on scroll direction
  if (delta > 0) {
    scroller.scrollBy({
      top: window.innerHeight,
      behavior: "smooth",
    });
  } else if (delta < 0) {
    scroller.scrollBy({
      top: -window.innerHeight,
      behavior: "smooth",
    });
  }
});

window.addEventListener("scroll", updateBackgroundColor);

I am getting nothing on the console

I am trying to change the background color whenever the user scrolls.

2

Answers


  1. The wheel event is not synonymous with the scroll event.

    From MDN:

    Note: Don’t confuse the wheel event with the scroll event. The default
    action of a wheel event is implementation-specific, and doesn’t
    necessarily dispatch a scroll event. Even when it does, the delta*
    values in the wheel event don’t necessarily reflect the content’s
    scrolling direction. Therefore, do not rely on the wheel event’s
    delta* properties to get the scrolling direction. Instead, detect
    value changes of scrollLeft and scrollTop of the target in the scroll
    event.

    use the scroll event and make sure the document is tall enough and with enough content for scrolling to be able to take place.

    addEventListener("scroll", function(event){
      console.log("scrolling");
    });
    <p>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>
    </p>
    Login or Signup to reply.
  2. problem is not in EventListener. I think it’s in your function! Can I see your console after scrolling and your HTML code, please.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search