2

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;
}
Brendan Jackson
  • 93
  • 1
  • 10
  • 3
    You require javascript for stopping the video. Either remove it from the DOM or use Youtube api to pause/stop the playing video – David Chelliah Jun 06 '16 at 19:26
  • html is not a programming language. nor is css. css has some MINOR capabilities, like reacting to mouseover events and whatnot, but it can only change FORMATTING of a page, it can't start/stop a video. – Marc B Jun 06 '16 at 19:28
  • okay I am looking into stopping the video with JS. I am less experienced with it and wanted to see if there was an easier way first, especially since JS can slow down the page. Thank You. – Brendan Jackson Jun 06 '16 at 19:29
  • There is a similar stackoverflow question with a solution, But that happens when hover on the video check http://stackoverflow.com/questions/20706799/start-youtube-video-on-hover-mouseover – David Chelliah Jun 06 '16 at 19:37
  • Thank you. I will try to use that to figure it out. – Brendan Jackson Jun 06 '16 at 19:40
  • @BrendanJackson check out the jsfiddle that I have created and mark as answer if it works out for your requirement. – David Chelliah Jun 07 '16 at 05:57

2 Answers2

1

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>
David Chelliah
  • 1,319
  • 1
  • 13
  • 24
  • I will try to mark an answer soon. This looks really promising, but I am still figuring out what I need to plug in. – Brendan Jackson Jun 07 '16 at 10:32
  • I don't understand how to connect this to my HTML file and what to put where. Is this just the one JS document or am I missing something it looks like a lot of variables are undefined. – Brendan Jackson Jun 07 '16 at 10:40
  • I think what I will actually do is start videos muted and find ones that work well without sound. As I have just learned I cannot connect external files. Not even .CSS! Thank you for your help. – Brendan Jackson Jun 07 '16 at 14:03
0

All I did was replace the src attribute in the iframe with nothin':

$('.youtube_div .close').on('click', function() {
    $('.youtube_div iframe').attr('src', '');
})

Seems to work.

Dave
  • 3,073
  • 7
  • 20
  • 33
  • Great job for answering how to fix the issue, you could improve your answer by explaining *why* this solution works. – Zach Jensz Mar 18 '22 at 06:18
  • Thanks for the tip, Zach. Unfortunately I'm not a browser engineer and I have no idea why the browser devs decided that the src attribute for an embedded iframe video should act like a play/stop toggle for videos. Perhaps it's in a spec somewhere. I basically tried it and it worked - that's all I can tell you. – Dallas Ransom Mar 19 '22 at 14:29