How do I control a video from Scroll?

Asked

Viewed 217 times

1

  • Ollie Rogers' Codepen works fine, except for the failure to detect correctly (at least in my Chrome + Ubuntu) when the scrolling started. What you would like to be different from this example?

2 answers

1


I leave here an explanation of what is happening in the codePen that you indicated.

// selecionar o elemento do video
var vid = document.getElementById('v0');
//var vid = $('#v0')[0]; // variante com jQuery

// pausar o video ao inicio
vid.pause();
 
// pausar o video ao detectar um scroll (para impedir auto-play)
window.onscroll = function(){
    vid.pause();
};

// procurar, a cada 40 milisegundos, o scroll atual da página e mostrar a frame com esse numero/400
setInterval(function(){
    vid.currentTime = window.pageYOffset/400;
}, 40);

If you want to change the code a little take a look at this variant:

Demo: http://jsfiddle.net/n3Lm6/

var vid = document.getElementById('v0');
vid.pause();
window.onscroll = function () {
    vid.currentTime = window.pageYOffset / 400;
    vid.pause();
};

document.getElementById('play').onclick = function () {
    vid.play();
}
document.getElementById('pause').onclick = function () {
    vid.pause();
    window.scrollTo(0, vid.currentTime * 400); // para colocar o scroll no sitio quando faz pause
}

Here I joined inside the scroll the part that was with setInterval and attached a button play and pause to illustrate how you can control the video.

-2

window.onscroll = function() {
myFunction()};
var video = document.getElementById('videotrailer');

function myFunction() {
  if (document.body.scrollTop > 350) {
    this.video.pause();
  }
   else{
    this.video.play();
  }
}
  • Explain your code a little bit and not just paste it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.