-4

So I have a pop over div that shows when going to the site with a YouTube video auto playing in there, the problem is, if the box is closed the audio of the video still remains, is there a way to get around this with jQuery? I don't know if there is a way to stop sound from a certain element?

The code is below.

CSS

.full_page_overlay1 {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0px;
    left: 0px;
    background: #626262;
    opacity: 0.9;
    z-index: 2147483646;
}

.cart_over1 {
    width: 1024px;
    position: fixed;
    float: left;
    border: 1px solid black;
    box-shadow: 1px 1px 10px black;
    background: white;
    z-index: 2147483647;
    border-radius: 10px;
}

#close_box1 {
    float: left;
    top: 0px;
    left: 0px;
    display: table-cell;
    vertical-align: middle;
    width: 40px;
    height: 40px;
    font-size: 40px;
    text-align: center;
    color: black;
    text-shadow: 1px 1px 3px black;
    font-weight: bolder;
    cursor: pointer;
    margin-bottom: 10px;
}

HTML/PHP

<?php

  if ($_COOKIE['video'] != 'watched') {

$value = "watched";

// send a simple cookie
setcookie("video",$value,time() + (10 * 365 * 24 * 60 * 60));

?>

   <div class="full_page_overlay1"></div>

  <div class="container">

  <div class="cart_over1">

      <div id="close_box1">X</div>

      <iframe width="1024" height="600" src="//www.youtube.com/embed/zDnVDyVYiv8?autoplay=1" frameborder="0" allowfullscreen></iframe>

  </div>
      </div>
      <?php } ?>

And the jQuery

$( document ).ready(function() {
    $("#close_box1").click(function(){
    $(".full_page_overlay1").fadeOut();
    $(".cart_over1").fadeOut();
})
});

Many thanks in advance for your help.

AppleTattooGuy
  • 1,145
  • 8
  • 17
  • 39

3 Answers3

2

As you're using iFrame, you can just clear its src:

$( document ).ready(function() {
    $("#close_box1").on("click", function () {
        $(".full_page_overlay1").fadeOut();
        $(".cart_over1").fadeOut().find("iframe").attr("src", "");
    })
});

If you're using the jQuery UI Dialog, you could set this to be done on the close event.

emerson.marini
  • 9,331
  • 2
  • 29
  • 46
2

Replace:

<iframe width="1024" height="600" src="//www.youtube.com/embed/zDnVDyVYiv8?autoplay=1" frameborder="0" allowfullscreen></iframe>

To:

<iframe width="1024" height="600" src="//www.youtube.com/embed/zDnVDyVYiv8?autoplay=1" frameborder="0" allowfullscreen id="clearmySound"></iframe>  

Replace Js Code:

$( document ).ready(function() {
    $("#close_box1").click(function(){
    $(".full_page_overlay1").fadeOut();
    $(".cart_over1").fadeOut();
    $("#clearmySound").attr('src','');      
})
});  

JSFIDDLE DEMO

Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35
1

Change your iframe so it has an ID:

  <div id="close_box1">X</div>

  <iframe width="1024" height="600" ID="autoplayvideoframe" src="//www.youtube.com/embed/zDnVDyVYiv8?autoplay=1" frameborder="0" allowfullscreen></iframe>

Then remove that element when the window closes:

$( document ).ready(function() {
    $("#close_box1").click(function(){
    $(".full_page_overlay1").fadeOut();
    $(".cart_over1").fadeOut();
    $("#autoplayvideoframe").remove();
})
});

Alternatively you could just use one of the pre-existing modal popups which handle this kind of thing for you. Setting the iframe to be a fixed huge size like that means you're going to give a poor experience with tablets and mobile.

rtpHarry
  • 13,019
  • 4
  • 43
  • 64