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

105
Visualizações
Select multiple elements with document.querySelector

I'm trying to have multiple videos to hover over and start a preview video like youtube. Works so far, but only for one video, the first one. How do I make it play on hover for every video? (The videos are the same for testing)

HTML:

<div class="videos" onclick="location.href='video1.html'" style="cursor:pointer; position: relative;">
        <video>
            <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" />
        </video>
        <b>Video Title</b>
        <p>Video Description</p>
</div>
<div class="videos" onclick="location.href='video2.html'" style="cursor:pointer; position: relative;">
        <video>
            <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" />
        </video>
        <b>Video Title 2</b>
        <p>Video Description 2</p>
</div>

JS:

function delay(milliseconds){
            return new Promise(resolve => {
                setTimeout(resolve, milliseconds);
            });
        }
                
        const video = document.querySelector("video");
        
        async function startPreview() {
          await delay(300);
          video.muted = true;
          video.currentTime = 1;
          video.playbackRate = 0.5;
          video.play();
        }
        
        function stopPreview() {
          video.currentTime = 0;
          video.playbackRate = 1;
          video.pause();
        }
        
        let previewTimeout = 1000;
        
        video.addEventListener("mouseenter", () => {
          startPreview();
          previewTimeout = setTimeout(stopPreview, 4000);
        });
        
        
        video.addEventListener("mouseleave", () => {
          clearTimeout(previewTimeout);
          previewTimeout = null;
          stopPreview();
        });

When I change "document.querySelector" to "document.querySelectorAll", nothing will happen at all and the first video doesn't play when hovering anymore.

How it looks rn

about 4 years ago · Juan Pablo Isaza
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