【发布时间】:2016-10-12 07:19:53
【问题描述】:
当鼠标在 x 轴上移动时,我正在尝试使用一些 Javascript 来触发视频播放,但无法让它工作。
我已经包含了an example 我想要实现的目标,以及使之成为可能的代码。
var video = document.getElementById('video');
var x = window.innerWidth / 2;
var y = 0;
var loaded = false;
document.onclick = function(e) {
window.parent.postMessage('feature:click', '*');
};
// function elementAtMousePosition() {
// return document.elementFromPoint(x, y);
// }
// document.addEventListener('click', function(event) {
// var newEvent = new Event(event.type);
// elementAtMousePosition().dispatchEvent(newEvent);
// });
document.onmousemove = function(vent) {
event = event || window.event;
x = event.clientX;
y = event.clientY;
if (loaded) {
throttledSeek();
}
};
var throttle = function(delay, callback) {
var previousCall = new Date().getTime();
return function() {
var time = new Date().getTime();
if ((time - previousCall) >= delay) {
previousCall = time;
callback.apply(null, arguments);
}
};
};
var seek = function() {
var spins = 3;
var pos = (x - (window.innerWidth / spins * 0.5)) / (window.innerWidth / spins);
pos -= Math.floor(pos);
video.currentTime = pos * video.duration;
};
var throttledSeek = throttle(1000 / 16, seek);
var onload = function() {
coverVid(video, video.videoWidth, video.videoHeight);
loaded = true;
video.style.opacity = 1;
window.onresize();
seek();
};
video.load();
video.addEventListener("canplaythrough", function() {
this.play();
this.pause();
onload();
});
我尝试使用 jQuery 来做类似的事情,但是 .mousemove() 在 Chrome 中的触发频率不够高,无法实现这一目标。想知道我错过了什么/遗漏了什么/完全愚蠢,这使得我在上面提供的代码和示例打勾。任何建议、建设性批评或指点都将不胜感激!
【问题讨论】:
标签: javascript jquery css html html5-video