我已经在悬停时实现了Vimeo视频并且工作正常,但问题是当我将鼠标悬停在该div上时视频正常播放但是当我将鼠标悬停在该div之外时它不会暂停(mouseout)。
请查看以下Js代码:
$(document).on('mouseenter','.play-video',function(e){
e.preventDefault();
var videoUrl = $(this).attr('href');
//alert(videoUrl);
$('#videoBox_'+videoUrl).show();
$(this).next('#videoBox_'+videoUrl).html('<iframe id="player_'+videoUrl+'" src="http://player.vimeo.com/video/' + videoUrl + '?api=1&player_id=player_'+videoUrl+'&autoplay=1" frameborder="0" allowfullscreen></iframe>');
var player = $("#player_"+videoUrl);
//console.log(player[0].id);
player.mouseover(function(){
froogaloop = $f(player[0].id);
froogaloop.api('play');
});
player.mouseout(function(){
froogaloop = $f(player[0].id);
froogaloop.api('pause');
});
});
答案 0 :(得分:1)
我想为此案提出另一种选择。为了播放/暂停某些vimeo视频片段,我们可以像这样调整一些给定的代码:
示例参考代码:
$(document).ready(function() {
// 1) Assign a given variable called oPlayer
var oPlayer = undefined;
$('.container .item').on('mouseenter', '.play-video', function(e) {
e.preventDefault();
// 2) When user hover the hovered [iframe], The oPlayer
// will now be assigned to that given [iframe] element
oPlayer = $(this);
froogaloop = $f(oPlayer[0].id);
froogaloop.api('play');
}).mouseleave(function() {
// 3) So by the time the user leaves that
// iframe, It will pause that given [iframe]
froogaloop = $f(oPlayer[0].id);
froogaloop.api('pause');
});
});
以下是jsfiddle以供进一步参考:https://jsfiddle.net/85dLbt2u/5/
希望这有助于你的案例。