Search code examples
htmlcssyoutube

Stop a youtube video in an iframe when the div it is in is display:none


I have a div, "c1" which is display:none except when div "p1" is hovered. In "c1" there is a YouTube video set to auto play. in chrome when the div is visible the video starts but when it disappears the video keeps playing. Is there a way to stop it with only HTML and CSS or do I need 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;
}

Solution

  • I adopted the script shared earlier in the comment and I have created an alternate version to play video on mouseenter on an element and stopping when moving out of the element.

    http://jsfiddle.net/588vwkrw/6/

    Code for your reference (But use the JSfiddle, as the external youtube script link is not working within the SO snippet widget )

    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();
    <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>