我有一个选择的视频缩略图,我想触发播放/暂停在悬停。我设法让他们中的一个开始工作,但我遇到了与名单上其他人的问题。附件是我密码的小提琴。会有一个div覆盖每个html5视频,所以悬停需要委托给视频,我不知道该如何做。
https://jsfiddle.net/meh1aL74/
在这里预览html -
<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 -
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();
}非常感谢你的帮助。
发布于 2014-11-06 12:26:05
为什么要将本机事件绑定到jQuery中?
无论如何,如果您想以本地方式处理事件,可以使用方法并将每个视频的索引传递给处理程序。
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();
}http://jsfiddle.net/gaby/0o8tt2z8/2/演示
或者你可以使用完整的jQuery
var figure = $(".video").hover( hoverVideo, hideVideo );
function hoverVideo(e) { $('video', this).get(0).play(); }
function hideVideo(e) { $('video', this).get(0).pause(); }http://jsfiddle.net/gaby/0o8tt2z8/1/演示
发布于 2017-06-06 19:33:24
最短版本将是这个版本。不需要任何花哨的jQuery,只是普通的HTML:
<div>
<video onmouseover="this.play()" onmouseout="this.pause();this.currentTime=0;">
<source src="yourVideo.ogg" type="video/ogg"></source>
</video>
</div>这样它就有点干净了。
发布于 2014-11-06 11:55:18
hoverVideo()函数专门调用.thevideo的第一个实例,因此悬停在其中任何一个上都会播放第一个视频。
您必须获取事件发生的元素,然后在该元素中找到.thevideo元素:
var figure = $(".video");
var vid = $("video");
[].forEach.call(figure, function (item) {
item.addEventListener('mouseover', hoverVideo, false);
item.addEventListener('mouseout', hideVideo, false);
});
function hoverVideo(e) {
$(this).find('.thevideo')[0].play();
}
function hideVideo(e) {
$(this).find('.thevideo')[0].pause();
}这里有一个更新的小提琴: http://jsfiddle.net/52mxdbgy/1/
https://stackoverflow.com/questions/26778714
复制相似问题