问题
I have four vimeo iframes in a page. I want to play the video on mouseover and pause the video on mouseout. The following code works, but only for the last video. How can I modify it so that it will work for all the videos?
#wrapper {
width: 85%;
margin-left: auto;
margin-right: auto;
padding-top: 50px;
}
#wrapper .card {
width: 25%;
float: left;
box-sizing: border-box;
}
<div id="wrapper">
<div class="card">
<iframe class="product-card-media" id="player1" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player1"></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player2" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player2"></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player3" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player3"></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player4" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player4"></iframe>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<script>
$('.product-card-media').each(function() {
var player = $("#" + this.id);
froogaloop = $f(player[0].id);
player.mouseover(function() {
froogaloop.api('play');
}).mouseout(function() {
froogaloop.api('pause');
});
});
</script>
回答1:
The following code works perfectly for me
$('.product-card-vimeo-video').mouseover(function(){
var player = $("#" + this.id);
froogaloop = $f(player[0].id);
froogaloop.api('play');
player.mouseout(function(){
froogaloop.api('pause');
});
});
回答2:
Try like this:
$('.product-card-media').each(function(){
var player = $(".product-card-media");
froogaloop = $f(player[0].id);
player.mouseover(function(){
froogaloop.api('play');
}).mouseout(function(){
froogaloop.api('pause');
});
});
Check the PEN: http://codepen.io/anon/pen/wMLmVQ
回答3:
Instead of using $.each that will loop through every id you should use $('#id).on('mouseover',function(){ });
Please see the following working code
$('.product-card-media').on('mouseover',function(){
var player = $("#"+this.id);
froogaloop = $f(player[0].id);
player.mouseover(function(){
froogaloop.api('play');
}).mouseout(function(){
froogaloop.api('pause');
});
});
#wrapper{
width:85%;
margin-left:auto;
margin-right:auto;
padding-top:50px;
}
#wrapper .card{
width:25%;
float:left;
box-sizing:border-box;
}
<div id="wrapper">
<div class="card">
<iframe class="product-card-media" id="player1" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player1" ></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player2" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player2" ></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player3" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player3" ></iframe>
</div>
<div class="card">
<iframe class="product-card-media" id="player4" type="text/html" width="100%" src="http://player.vimeo.com/video/126309467?api=1&player_id=player4" ></iframe>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
来源:https://stackoverflow.com/questions/35549780/play-vimeo-videos-on-mouse-hover