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

185
Visualizações
Adding a class to a div when the div is on scrolled into the screen

So I am trying to add the fadeInTop class to the div with the class animation. The Javascript is not working correctly...

The console returns me this which goes back to that line: document.querySelectorAll(".animated".classList.add(fadeInTop))

Uncaught TypeError: Cannot read properties of undefined (reading 'add')
    at application.js:41:1
    at Array.forEach (<anonymous>)
    at IntersectionObserver.<anonymous> (application.js:39:1)

This is in the application.js file:

import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"


const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if(entry.isIntersecting){
      document.querySelectorAll(".animated".classList.add(fadeInTop))
    }
  })
})

observer.observe(document.querySelector(".containerFadeIn"))

This is from the home.html.erb view:

<main class="home-main">
<%# fist box %>

  <div class="home-text-1 containerFadeIn">
    <div class="row containerFadeIn">
      <div class="col-md-9 d-flex animated">
        <div class="vendo-logo-home">
          <%= image_tag "vendo-logo.png"%>
        </div>
        <div>
          <h1>The Future begins</h1>
          <h1 > with <span class="light-blue"><strong>VENDO</strong></span></h1>
        </div>
      </div>
    </div>
  </div>
</main>

my main goal is to make the text fade into the screen when the text is scrolled into the screen (if i add the fadeInTop class to the the fade in animation works)

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

0

This doesn't look right document.querySelectorAll(".animated".classList.add(fadeInTop))

  • If you are calling document.querySelectorAll this should return node list so you need to iterate through all elements like:

document.querySelectorAll(".animated").forEach(element => element.classList.add("fadeInTop"))

Keep in mind that nodelist is not an array but it does accept forEach method. You might want to convert that into array like Array.from(document.querySelectorAll(".animated")) and then call forEach method.

  • If you have only one element then you can call:

document.querySelector(".animated").classList.add("fadeInTop")

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