Skip to content

Instantly share code, notes, and snippets.

@isramv
Created November 3, 2017 18:39
Show Gist options
  • Select an option

  • Save isramv/338fad9436da1a38aec61ca901a5568a to your computer and use it in GitHub Desktop.

Select an option

Save isramv/338fad9436da1a38aec61ca901a5568a to your computer and use it in GitHub Desktop.
vision bxSlider MP4 and Youtube autoplay
// working with bxSlider.js
// Youtube and MP4 autoplay videos on Header Sliders.
(function($) {
var CB = {
slider: $('.bxslider'),
video: {},
youtubeVideos: [],
onLoad: function() {
var videoItem = CB.slider.get(0).children.item(1);
// check first slide to check if youtube video.
if (CB.isYoutubeVideo(videoItem)) {
// console.info('Has ha youtube video πŸ‘»');
CB.playYoutubeVideo(videoItem);
}
// console.info('Check if first slide is a video or not');
if (CB.hasVideo(CB.slider.get(0).children.item(1))) {
CB.slider.stopAuto();
CB.playVideo(CB.slider.get(0).children.item(1));
} else {
CB.slider.startAuto();
}
},
start: function() {
this.slider.bxSlider({
auto: false,
onSliderLoad: this.onLoad,
onSlideBefore: this.onSlideBefore,
onSlideNext: this.onSlideNextOrPrev,
onSlidePrev: this.onSlideNextOrPrev
});
},
onSlideBefore: function(destinationElement) {
if (CB.hasImage(destinationElement)) {
CB.slider.startAuto();
} else {
CB.slider.stopAuto();
}
},
onSlideNextOrPrev: function(nextObjext) {
// console.info('check video status πŸ‘€');
CB.pauseAllVideos();
//get current slice and see if video is playing.
var currentSlide = nextObjext.prevObject.get(CB.slider.getCurrentSlide());
// check if youtube video.
if (CB.isYoutubeVideo(currentSlide) && typeof YT !== 'undefined' && YT.loaded) {
CB.youtubeCurrentSlideActions(currentSlide);
} else if (CB.hasVideo(currentSlide)) {
CB.playVideo(currentSlide);
}
},
youtubeCurrentSlideActions: function(currentSlide) {
// check CB array of videos if exists pause it.
var currentSlideId = $(currentSlide).find('iframe').get(0).id;
var video = CB.youtubeVideos.filter(function(item, index) {
if (item.a.id === currentSlideId) {
return item.a;
}
});
// play the video if exists.
if (video.length > 0) {
video[0].playVideo();
} else if (typeof YT !== 'undefined' && YT.loaded) {
CB.playYoutubeVideo(currentSlide);
}
},
pauseAllVideos: function() {
// stop MP4 videos.
$.each(CB.slider.find('video'), function(evt) {
this.pause();
});
// stop Youtube videos.
if (CB.youtubeVideos.length > 0) {
// stop all youtube videos.
$.each(CB.youtubeVideos, function(index, video) {
video.pauseVideo();
});
}
},
isYoutubeVideo: function(videoItem) {
if (this.hasVideo(videoItem)) {
if ($(videoItem).children().hasClass('file-video-youtube')) {
return true;
}
}
return false;
},
playVideo: function(videoItem) {
// console.info('let\'s play a video 🍿');
this.video = $(videoItem).find('video').get(0);
if (typeof this.video !== 'undefined') {
this.video.loop = false;
this.video.controls = true;
this.video.play();
$(this.video).on('ended', this.onVideoEnded);
}
},
playYoutubeVideo: function(videoItem) {
// console.info('Play youtube video 🍿');
if(typeof YT !== 'undefined') {
var currentVideoId = $(videoItem).find('iframe').prop('id');
var youtubeVideo = new YT.Player(currentVideoId,{
events: {
onReady: this.onYoutubePlayerReady,
onStateChange: this.onYoutubePlayerStateChange
}
});
CB.youtubeVideos.push(youtubeVideo);
}
},
onYoutubePlayerReady: function(event) {
event.target.playVideo();
},
onYoutubePlayerStateChange: function(event) {
// console.info('Youtube player state changed');
if (event.data === 0) {
// if youtube video ended stop and go to next slide.
event.target.stopVideo();
CB.slider.goToNextSlide();
}
},
hasImage: function(destinationElement) {
if ($(destinationElement).children().hasClass('file-image')) {
return true;
}
return false;
},
hasVideo: function(currentSlide) {
if ($(currentSlide).children().hasClass('file-video')) {
return true;
}
return false;
},
onVideoEnded: function() {
// console.info('video ended πŸ™ƒ');
CB.slider.goToNextSlide();
}
};
// Youtube custom.
var YTC = {
attempts: 0
};
// If page-node.
if ($('body').hasClass('page-node')) {
// check if youtube video exists.
if ($('.media-youtube-player').length) {
var tag = document.createElement('script');
tag.id = 'slider-youtube-embbed';
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
var slider_timer = setInterval(function() {
if (typeof YT !== 'undefined' && YT.loaded) {
// console.info('Youtube is ready πŸ‘ŠπŸ½ clearing Interval.');
clearInterval(slider_timer);
// Start slider.
CB.start();
} else if (YTC.attempts >= 60) {
console.error('Too many attempts to youtube, autoplay has been disabled.');
clearInterval(slider_timer);
CB.start();
}
YTC.attempts++;
}, 100);
} else {
// Other sliders not specifically configured above
$('.bxslider').each(function(idx, el) {
if ($(el).children().length > 1 && $(el).hasClass('initialized') === false) {
$(el).bxSlider().addClass('initialized');
}
});
}
// End of slider Videos autoplay.
}
)(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment