Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
How do you change the background colour on scroll?

I'm trying to change the background colour of my webpage on scroll using IntersectionObserver- this is the code I have so far:

const callback = (entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        style.backgroundColor = entry.target.getAttribute('data-color');
      }
    });
  };
  
  const changes = document.querySelectorAll('.change');

  const observer = new IntersectionObserver(callback);

  changes.forEach(change => {
    observer.observe(change);
});

I would like to have it change colour when the viewer scrolls to a div, possibly using something like

<div id= "indiaText" class="change" data-color="orange">

Does anyone have an idea of how I can achieve this? I have also tried $(window).scroll(function() { however I only get an error using this method. Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You almost had it.

The only real problem was that the style.backgroundColor = line was throwing an exception because style wasn't declared/defined. i changed it to document.body.style.backgroundColor.

I tweaked a couple of other things, but these aren't critical:

  • i added an options argument { threshold: .5 } to the IntersectionObserver so the color change doesn't occur until the target div is 50% on screen.
  • i added a css transition on body just to soften the color change.

const callback = (entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      document.body.style.backgroundColor = entry.target.dataset.color;
    }
  });
};

const changes = document.querySelectorAll('.change');

const observer = new IntersectionObserver(callback, { threshold: .5 });

changes.forEach(change => {
  observer.observe(change);
});
body {
 transition: .5s background-color;
}

.change {
  min-height: 100vh;
  background-color: white;
  width: 50%;
  margin: 40px auto;
}
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>
<div class="change" data-color="orange">orange</div>
<div class="change" data-color="blue">blue</div>
<div class="change" data-color="lavender">lavender</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Just update the callback function:


if (entry.isIntersecting) {

 document.body.style.background = entry.target.getAttribute('data-color');

 //to change the background of the div
 // entry.target.style.background = entry.target.getAttribute('data-color');
 
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda