问题描述
我有一系列视频缩略图,我想在悬停时触发播放/暂停。我设法让其中一个工作,但我遇到了列表中其他人的问题。附件是我的代码的小提琴。每个html5视频都会有一个div,所以悬停需要委托给视频,我不知道该怎么做。
I have a selection of video thumbnails that I want to trigger to play/pause on hover. I have managed to get one of them to work, but I run into a problem with the others on the list. Attached is the fiddle of my code. There will be a div covering each html5 video so the hover needs to delegate to the video, which i'm unsure as to how to do.
预览html这里 -
Preview of the html here -
<div class="video">
<div class="videoListCopy">
<a href="videodetail.html" class="buttonMore">
<div class="breaker"><div class="line"></div></div>
<div class="buttonContent">
<div class="linkArrowContainer">
<div class="iconArrowRight"></div>
<div class="iconArrowRightTwo"></div>
</div>
<span>Others</span>
</div>
</a>
</div>
<div class="videoSlate">
<video class="thevideo" loop>
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
</div>
<div class="video">
<div class="videoListCopy">
<a href="videodetail.html" class="buttonMore">
<div class="breaker"><div class="line"></div></div>
<div class="buttonContent">
<div class="linkArrowContainer">
<div class="iconArrowRight"></div>
<div class="iconArrowRightTwo"></div>
</div>
<span>Others</span>
</div>
</a>
</div>
<div class="videoSlate">
<video class="thevideo" loop>
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
</div>
在这里预览javascript -
Preview of the javascript here -
var figure = $(".video");
var vid = $("video");
[].forEach.call(figure, function (item) {
item.addEventListener('mouseover', hoverVideo, false);
item.addEventListener('mouseout', hideVideo, false);
});
function hoverVideo(e) {
$('.thevideo')[0].play();
}
function hideVideo(e) {
$('.thevideo')[0].pause();
}
非常感谢你的帮助。
Oliver
推荐答案
为什么你将本机事件与jQuery绑定在一起?
Why are you uisng native event binding together with jQuery ?
无论如何,如果你想本地处理事件,你可以使用并将每个视频的索引传递给处理程序
Anyway, if you want to handle the events natively you can use the .bind
method and pass the index of each video to the handlers
var figure = $(".video");
var vid = figure.find("video");
[].forEach.call(figure, function (item,index) {
item.addEventListener('mouseover', hoverVideo.bind(item,index), false);
item.addEventListener('mouseout', hideVideo.bind(item,index), false);
});
function hoverVideo(index, e) {
vid[index].play();
}
function hideVideo(index, e) {
vid[index].pause();
}
演示
或者你可以完全jQuery
Or you can go full jQuery
var figure = $(".video").hover( hoverVideo, hideVideo );
function hoverVideo(e) { $('video', this).get(0).play(); }
function hideVideo(e) { $('video', this).get(0).pause(); }
演示
这篇关于悬停时的视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!