Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 video play by scrolling with the mouse wheel

I want to be able to make a web page that plays a video forward and backward when they scroll with the mouse wheel up and down. It seems conceivable, via HTML5 and possibly JavaScript. Any sort of direction for this sort of thing would be helpful.

like image 363
Mike Preble Avatar asked Nov 16 '12 15:11

Mike Preble


People also ask

How do I assign clicking to my scroll wheel?

Go to the normal mouse tab, add a new button, go to the "click here to select mouse button" area and scroll the wheel. It will capture that action and you may assign it to what you want.


2 Answers

Pause the video at all times. get scroll position with regular intervals and make the video seek to the scroll position. using any page loader effects to let the video fully buffer is recommended though.

full code

// select video element
var vid = document.getElementById('v0');
//var vid = $('#v0')[0]; // jquery option

// pause video on load
vid.pause();

// pause video on document scroll (stops autoplay once scroll started)
window.onscroll = function(){
    vid.pause();
};

// refresh video frames on interval for smoother playback
setInterval(function(){
    vid.currentTime = window.pageYOffset/400;
}, 40);
like image 96
Nav Avatar answered Sep 21 '22 10:09

Nav


I know this is an old question, but I stumbled across it the other day and the answer above helped me write a little jQuery plugin to achieve this effect.

On scroll I calculated the position of the video element in relation to the window, then used that to calculate the current time on the video.

However instead of using an setTimeout/setInterval to update the current time of the video, I used request animation frame. Request animation frame will render the video when it can instead of using a setInterval which will run even if the browser is not ready.

Applying this to the above example:

var renderLoop = function(){
  requestAnimationFrame( function(){
    vid.currentTime = window.pageYOffset/400;
    renderLoop();
  });
};
renderLoop();

Supported in all modern browsers except Opera Mini.

like image 28
Paul. B Avatar answered Sep 19 '22 10:09

Paul. B