Answer the question
In order to leave comments, you need to log in
How to stop youtube video in js?
Greetings.
I bought one slider plugin for WP. I managed to somehow make the slider from the category display the video, not the preview. But there is a minor nuisance)
I have 7 slides in the slider. Each is an embed video from youtube. They don't play automatically. But now I’m thinking about what to add to the plugin’s JS so that when switching a slide, the video from the previous slide stops by itself.
This may not be very critical, because. The video is no longer than two minutes, but still. If a person turned on one video, went to another slide and turned on another, then somehow it’s not cool.
I will be glad if you advise.
Answer the question
In order to leave comments, you need to log in
There was a similar situation, it was necessary to play when the modal window opens, and if you close it, click on the pause, I found a solution!
<script>
$(document).on('click', '#close_vid', function() {
jQuery("iframe").each(function() {
jQuery(this)[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')
});
});
</script>
<script>
$(document).on('click', '#play_video', function() {
jQuery("iframe").each(function() {
jQuery(this)[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*')
});
});
</script>
<script>
function toggleVideo(state) {
// if state == 'hide', hide. Else: show video
var div = document.getElementById("popupVid");
var iframe = div.getElementsByTagName("iframe")[0].contentWindow;
div.style.display = state == 'hide' ? 'none' : '';
func = state == 'hide' ? 'pauseVideo' : 'playVideo';
iframe.postMessage('{"event":"command","func":"' + func + '","args":""}', '*');
}
</script>
<p><a href="javascript:;" onClick="toggleVideo();">Click here</a> to see my presenting showreel, to give you an idea of my style - usually described as authoritative, affable and and engaging.</p>
<!-- popup and contents -->
<div id="popupVid" style="position:absolute;left:0px;top:87px;width:500px;background-color:#D05F27;height:auto;display:none;z-index:200;">
<iframe width="500" height="315" src="http://www.youtube.com/embed/T39hYJAwR40?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<br /><br />
<a href="javascript:;" onClick="toggleVideo('hide');">close</a>
You can hang something like this
var videoSrc = $("#Player1").attr("src");
$("#Player1").attr("src","");
$("#Player1").attr("src",videoSrc);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question