我有一个div,“c1”是display:none,除非div“p1”悬停。在“c1”中有一个YouTube视频设置为自动播放。当视频开始时div显示为chrome,但当视频消失时,视频会继续播放。有没有办法只使用HTML和CSS来阻止它,还是我需要JS?
HTML:
<div class="p1 parent"></div>
<div class="c1 child"><iframe class="embededInChild" src="https://www.youtube.com/embed/VLrULbtD-oo?autoplay=1" frameborder="1"></iframe>
<p class="childDescription">enjoy beautiful downtown bethesda just X miles from campus!</p></div>
CSS:
.child {
display: none;
width:494px;
height:300px;
position:absolute;
background-color: #E3EBF1;
border-radius: 10px;
border: 1px solid #333333;
left:0px;
top:-320px;
}
.parent{
background-color: #7BB9E0;
width:110px;
height:110px;
border:2px solid black;
display: block;
border-radius:55px;
margin: 5px;
display: inline-block;
float:left;
}
.parent:hover{
background-color: #CCCCCC;
border-color: #93305D;
}
.embededInChild{
width:474px;
height:250px;
margin: 5px 10px 0px 10px;
}
p.childDescription{
line-height:12px;
margin:5px 5px 0px 5px;
padding:0px;
color: #333333;
text-align: center;
}
.p1:hover + .c1 {
display: block;
}
答案 0 :(得分:1)
我采用了评论中早先分享的脚本,我创建了一个替代版本,用于在元素上的mouseenter上播放视频,并在移出元素时停止。
http://jsfiddle.net/588vwkrw/6/
代码供您参考(但使用JSfiddle,因为外部youtube脚本链接在SO片段小部件中不起作用)
var players = []; // an array where we stock each videos youtube instances class
$(function() {
var playvideo = function() {
var id = $(this).attr("href");
$(id).show();
var pid = id.replace("#", "")
players[pid].playVideo();
}
var stopvideo = function() {
var id = $(this).attr("href");
$(id).hide();
var pid = id.replace("#", "")
players[pid].stopVideo();
}
$(".playvid").mouseenter(playvideo).mouseleave(stopvideo);
});
function onYouTubeIframeAPIReady() {
var videos = $('iframe'), // the iframes elements
playingID = null; // stock the current playing video
for (var i = 0; i < videos.length; i++) // for each iframes
{
var currentIframeID = videos[i].id; // we get the iframe ID
//alert(currentIframeID);
players[currentIframeID] = new YT.Player(currentIframeID); // we stock in the array the instance
}
}
onYouTubeIframeAPIReady();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script async="false" type="text/javascript" src="https://www.youtube.com/player_api"></script>
<div>
Hover on the anchor links to play related videos</div>
<a class="playvid" href="#player"> Play video 1 </a>
<br/>
<br/>
<br/>
<a class="playvid" href="#player2"> Play Jquery video</a>
<br/>
<br/>
<br/>
<iframe style="display:none" id="player" width="385" height="230" src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe>
<iframe style="display:none" id="player2" width="385" height="230" src="https://www.youtube.com/embed/hMxGhHNOkCU?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe>
&#13;