hendy0817
hendy0817

Reputation: 1079

bootstrap 4 modal playing iframe when page loads?

My site has a full screen BG video on load which is the same video I want to play when user clicks play button to open modal. My issue is right when the page loads the modal video (iframe) is playing in the background. I do not want this. I want page loads > muted BG video >user clicks play > opens modal >plays video and stops when it closes. I tried using autoplay=0 which works if directing to a youtube src video but this video is in my project folder.

 <!-- banner -->
<section class="vid">
 <div class="video-container">
   <video class="bg-video" autoplay muted loop>
     <source src="video/americandream.mp4" type="video/mp4">
     Your browser is not supported
  </video>
</div>
  <div class="container-fluid banner">
    <div class="row">
    <div class="col-lg-12 motto text-white">
        <h1 class="banner-heading">
         <span class="overlay-heading">We Believe <br> People Make<br> The Difference</span>
          </h1>
          <div class="d-flex align-items-center mt-4">
            <a class="btn btn-primary btn-demo-top btn-wide demo mb-2 mr-md-2" href="demo.html">Book a Demo</a>

            <a href="#" class="launch-modal media align-items-center u-media-player" data-modal-id="modal-video">
              <span class="d-flex mr-2 ml-3">
                <span class="u-media-player__icon u-media-player__icon--success">
                  <span class="fa fa-play u-media-player__icon-inner"></span>
                </span>
              </span>
              <span class="media-body text-white  u-media-player__icon--success">
                Play Video
              </span>

            </a>
          </div>

                </div>
            </div>
            </div>
      </section>
  <!-- end of Banner -->


  <!-- MODAL -->
  <div class="modal fade" id="modal-video" tabindex="-1" role="dialog" 
   aria-labelledby="modal-video-label">
    <div class="modal-dialog modal-lg" role="document"> 
       <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" 
  aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <div class="modal-video">
                <div class="embed-responsive embed-responsive-16by9">
                    <iframe class="embed-responsive-item" 
            src="video/americandream.mp4"       webkitallowfullscreen- 
 two mozallowfullscreen- 
             two allowfullscreen-two></iframe>
                </div>
               </div>
            </div>
          </div>
       </div>
   </div>
   <!--END MODAL -->

Upvotes: 1

Views: 1102

Answers (1)

D. Stevens
D. Stevens

Reputation: 74

Im not sure if you're required to use an iframe to play your video file, but assuming you are not this example using the <video> tag should have the functionality you are describing.

$('.launch-modal').click(function() {
  $('.modal').fadeIn(200, function() {
  	$('#sample-video').get(0).play()
  });
});

$('.close').click(function() {
  $('.modal').fadeOut(100, function() {
  	$('#sample-video').get(0).pause();
  });
});
.bg-video {
  position: fixed;
  right: 0;
  bottom: 0;
  min-width: 100%;
  min-height: 100%;
  z-index: -1;
}

.banner {
  position: relative;
  background-color: #fff;
  text-align: center;
  padding: 20px;
  margin: 250px 50px 0px;
}

.modal {
  display: none;
  position: fixed;
  width: 33.3%;
  left: 33.3%;
  top: 200px;
}


.modal video {
  max-width: 100%;
  max-height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- banner -->
<section class="vid">
  <div class="video-container">
    <video class="bg-video" autoplay muted loop>
     <source src="https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
     Your browser is not supported
  </video>
  </div>
  <div class="container-fluid banner">
    <div class="row">
      <div class="col-lg-12 motto text-white">
        <h1 class="banner-heading">
          <span class="overlay-heading">We Believe <br> People Make<br> The Difference</span>
        </h1>
        <div class="d-flex align-items-center mt-4">
          <a class="btn btn-primary btn-demo-top btn-wide demo mb-2 mr-md-2" href="demo.html">Book a Demo</a>

          <a href="#" class="launch-modal media align-items-center u-media-player" data-modal-id="modal-video">
              <span class="d-flex mr-2 ml-3">
                <span class="u-media-player__icon u-media-player__icon--success">
                  <span class="fa fa-play u-media-player__icon-inner"></span>
                </span>
              </span>
              <span class="media-body text-white  u-media-player__icon--success">
                Play Video
              </span>

            </a>
        </div>

      </div>
    </div>
  </div>
</section>
<!-- end of Banner -->


<!-- MODAL -->
<div class="modal fade" id="modal-video" tabindex="-1" role="dialog" aria-labelledby="modal-video-label">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
      </div>
      <div class="modal-body">
        <div class="modal-video">
          <div class="embed-responsive embed-responsive-16by9">
            <video id="sample-video">
     <source src="https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
     Your browser is not supported
  </video>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<!--END MODAL -->

Upvotes: 1

Related Questions