Reputation: 57
I am trying to pause a youtube video when a slide changes using swiper from iDangerous
I have the youtube video urls ending with enablejsapi=1
and here is some code, the important part being slideChange
:
var swiper_video = new Swiper('.swiper-video', {
loop: true,
autoplay: false,
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next.video-swiper',
prevEl: '.swiper-button-prev.video-swiper',
},
slideChange: function (el) {
console.log('1');
$('.swiper-slide').each(function () {
var youtubePlayer = $(this).find('iframe').get(0);
if (youtubePlayer) {
youtubePlayer.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}
});
},
});
Can't seem to get the event to even fire, but I think the rest of the code should work.
Upvotes: 2
Views: 6089
Reputation: 5913
Your event looks like it is declared incorrectly, should be inside on:{}
, something like:
var swiper_video = new Swiper('.swiper-video', {
on: {
slideChange: function () {
console.log('Hello World')
},
}
});
So with your full code:
var swiper_video = new Swiper('.swiper-video', {
loop: true,
autoplay: false,
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next.video-swiper',
prevEl: '.swiper-button-prev.video-swiper',
},
on: {
slideChange: function (el) {
console.log('1');
$('.swiper-slide').each(function () {
var youtubePlayer = $(this).find('iframe').get(0);
if (youtubePlayer) {
youtubePlayer.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}
});
},
},
});
Check out the API docs for events here, or a working example here
Upvotes: 7