html5 video play by scrolling with the mouse wheel

后端 未结 5 916
無奈伤痛
無奈伤痛 2020-12-30 06:43

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

相关标签:
5条回答
  • 2020-12-30 06:48

    Possibly something like this

    $(document).mousewheel(function(event, delta, deltaX, deltaY){        
        if (deltaY > 0) {
            $(".video-element").get(0).playbackRate = -1;
        } else {
            $(".video-element").get(0).playbackRate = 1;
        }
    
        event.preventDefault();
    });
    
    0 讨论(0)
  • 2020-12-30 06:50

    Some mods from my end to make it smoother

    // 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(){
      TweenMax.to(vid,2,{currentTime:window.pageYOffset/400});
    }, 40);
    

    http://codepen.io/anon/pen/qORmee

    0 讨论(0)
  • 2020-12-30 06:59

    I'm using this. It's not perfect, but it should help you.

    var videoScrollTop = $(document).scrollTop();
    $(document).scroll(function() {
        var vid = $('#video')[0];
        if(videoScrollTop < $(document).scrollTop()){
            vid.currentTime += 0.081;
        } else {
            vid.currentTime -= 0.081;
        }
        videoScrollTop = $(document).scrollTop();
    });
    
    0 讨论(0)
  • 2020-12-30 07:02

    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.

    0 讨论(0)
  • 2020-12-30 07:06

    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);
    
    0 讨论(0)
提交回复
热议问题