【问题标题】:Triggering Events using Handler from html5 video player with JQuery使用带有 JQuery 的 html5 视频播放器中的处理程序触发事件
【发布时间】:2019-05-09 21:30:40
【问题描述】:

我正在使用这种方法(在堆栈溢出中找到)在清理 HMTL 视频时执行事件: Triggering Events from html5 video player with JQuery

完美运行。但是,我不知道如何在将视频时间线向后滑动到以前(较早)时间时触发不同的事件。我想关闭该事件,并可能在用户到达之前的原始时间时添加一个不同的事件。

我正在使用这种方法来“清理”时间线:

var windowwidth = $(window).width()-20;

var scrollpos = window.pageXOffset/200;
var targetscrollpos = scrollpos;
var accel = 0;

// ---- Values you can tweak: ----
var accelamount = 0.1; //How fast the video will try to catch up with the target position. 1 = instantaneous, 0 = do nothing.

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


window.onscroll = function(){
    //Set the video position that we want to end up at:
    targetscrollpos = window.pageXOffset/200;   
};

setInterval(function(){  
      //Accelerate towards the target:
      scrollpos += (targetscrollpos - scrollpos)*accelamount;
      //update video playback
      vid.currentTime = scrollpos;
     vid.pause();
}, 60);

这个代码来自之前的堆栈溢出问题来触发事件

    $(".boxB").animate({ marginLeft:'0px'},400);
    $(".boxA").animate({ width:'620px'}, 400);
}

var runAtTime = function(handler, time) {
     var wrapped = function() {
         if(this.currentTime >= time) {
             $(this).off('timeupdate', wrapped);
             return handler.apply(this, arguments);
        }
     }
    return wrapped;
};

$('#v0').on('timeupdate', runAtTime(myHandler, 3.5)); 

这里有一个工作版本: http://www.mediaflash.ca/swipe_video_timeline/index.html

该功能仅在您使用滚轮鼠标并在 iOS 设备上左右滚动或左右滑动时才有效。

【问题讨论】:

  • 所以你想知道用户是否及时向后或向前擦洗?只需将上次看到的时间与您的元素一起存储,并在下一次擦洗事件中检查新时间是否大于或小于之前的时间。这是你想做的吗?
  • 是的,我相信这正是我想要做的。我不知道该怎么做。你认为我可以挽救我目前使用的方法,还是应该完全使用不同的方法?我猜“如果(这个,currentTime

标签: html video timeline


【解决方案1】:

据我所知,没有可用于向前/向后滚动的单独事件。

检查用户是否向后滚动的方法是存储最后一次并将其与新的擦洗时间进行比较,如下所示:

var m_lastVideoTime = 0;//global scope

var runAtTime = function(handler, time) {

     var wrapped = function() {
         if (m_lastVideoTime < this.currentTime){
           //user scrolled back in time, do what you like here
         }
         m_lastVideoTime = this.currentTime;
         if(this.currentTime >= time) {
             //time exceeded specified value (3.5)
             $(this).off('timeupdate', wrapped);
             return handler.apply(this, arguments);
        }
     }
    return wrapped;
};

您当前的函数名称 runAtTime 有点误导,因为它会在视频时间更新时运行。但是,超过时间 3.5 时,它已经自行关闭。

从那一刻起,runAtTime 函数当前不会再次调用,直到页面重新加载,因为您只使用 .on jquery 函数附加了一次。

【讨论】:

  • 这看起来是一个有趣的解决方案,感谢您发布它。我已经尝试了各种实现它的方法,但完全不确定如何正确地做到这一点。我还在尝试
猜你喜欢
  • 2012-11-21
  • 2012-04-29
  • 2017-12-30
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
相关资源
最近更新 更多