Skip to content

Instantly share code, notes, and snippets.

@memandip
Last active September 1, 2019 07:19
Show Gist options
  • Select an option

  • Save memandip/e4695df396b34d837c797f17be88dee4 to your computer and use it in GitHub Desktop.

Select an option

Save memandip/e4695df396b34d837c797f17be88dee4 to your computer and use it in GitHub Desktop.
Pause Youtube Video on link click
<!DOCTYPE html>
<html>
<head>
<title>YouTube Video Pause</title>
<style type="text/css">
.col-md-8{
width: 75%;
float:left;
}
.col-md-4{
width:24%;
display: inline-block;
}
.hidden{
display: none;
}
</style>
</head>
<body>
<div class="col-md-8 multimedia-lg">
<div class="single-gallery-image-lg hidden" id="multimedia-1">
<iframe width="560" height="315" src="https://www.youtube.com/embed/HKS6cp5OGMo?enablejsapi=1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div class="single-gallery-image-lg hidden" id="multimedia-2">
<iframe width="560" height="315" src="https://www.youtube.com/embed/rzW0dPTDYsk?enablejsapi=1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
<div class="col-md-4">
<div className="multimedia-small">
<div class="gallery-nav-slide-single" >
<div class="ap-gallery-title" >
<h5>
<a href="" data-target="#multimedia-1">First Video</a>
</h5>
</div>
</div>
<div class="gallery-nav-slide-single">
<div class="ap-gallery-title">
<h5>
<a href="" data-target="#multimedia-2">Second Video</a>
</h5>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.ap-gallery-title a').on('click', function(e){
e.preventDefault()
let target= $(this).data('target')
$('.single-gallery-image-lg').addClass('hidden')
$(target).removeClass('hidden')
let iframes = $('.multimedia-lg').find('iframe')
$.each(iframes, function(i,d){
if($(d).get(0).contentWindow && $(d).get(0).contentWindow.postMessage){
$(d).get(0).contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*');
}
});
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment