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

196
Visualizações
Using 'this.currentTime' to get the time of a video and reset it to the starting point on 'hover out'

I have a video library where I want to dynamically use the Media Fragment time in URL as the poster. When hovering out, I am trying to reset the video to the initial start - to make sure the poster is at 2 seconds (in this specific example) instead of 0.

this.load works but creates a bad user experience as the whole video loads in again.

My idea is to define the current time as a variable (before the video starts playing) and use it when pausing the video.

However I just get "Uncaught ReferenceError: posterTime is not defined".

<video id="video">
  <source src="videourl.mp4#t=2" type="video/mp4">
</video>
const videos = document.querySelectorAll("video")

  videos.forEach(video => {

    video.addEventListener("mouseover", function () {
        var posterTime = this.currentTime;
        this.currentTime = 0;
        this.play()
    })
    
    video.addEventListener("mouseout", function () {
      this.currentTime = posterTime;
      this.pause();
    })
  })

Note that I use Webflow and is not very strong with jQuery/Javascript.

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

0

As I hover out I need it to show that initial frame again (not the first frame, but the one set in the URL)

Given this requirement you can retrieve the fragment from the URL in the src attribute of the source element and apply it to the currentTime of the video when the mouseleave event occurs:

const videos = document.querySelectorAll("video")

videos.forEach(video => {
  video.addEventListener("mouseenter", function() {
    this.currentTime = 0;
    this.play()
  })

  video.addEventListener("mouseleave", function() {
    let src = this.querySelector('source').src;
    let time = (src.split('#')[1] || 't=0').split('=')[1];
    this.currentTime = time;
    this.pause();
  })
})
<video id="video">
  <source src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4#t=5" type="video/mp4">
</video>

about 4 years ago · Juan Pablo Isaza Relatório

0

My idea is to define the current time as a variable (before the video starts playing) and use it when pausing the video. However I just get "Uncaught ReferenceError: posterTime is not defined".

Your idea and code is fine but you made a basic mistake. Remember: A variable defined inside a function will exist only for that function where it was created.

Use let for internal variables (where possible) and use var for global variables.

solution: Define the variable as global (outside of any functions)...

const videos = document.querySelectorAll("video");
var posterTime = -1; //# global var, with starting value...
 
videos.forEach(video => {

    video.addEventListener("mouseover", function () 
    {
        posterTime = this.currentTime; //# set time
        this.currentTime = 0;
        this.play()
    })

    video.addEventListener("mouseout", function () 
    {
        this.currentTime = posterTime; //# get time
        this.pause();
    })
})
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