Mpak91 Mpak91 - 1 year ago 386
Javascript Question

Owl Carousel 2 dynamically add videos

I'm trying dynamically add Owl Carousel videos to my site, but I ran into a problem; Videos appear on the page but when I try to play them console says

TypeError: video is undefined
width = video.width || '100%'

I've tried to follow instructions found on Owl Carousel documentation and some posts here on StackOverFlow, but can't figure this out. There are not many, if any, documentation about how to dynamically add videos in Owl Carousel.

My current code is following:

//init carousel
var owl = $('.owl-carousel');
touchDrag: true,
startPosition: 1,

var html = `<div data-merge="1" class="item-video">
<div class="owl-video-wrapper">
<a class="owl-video" href="" style="display: none;"></a>
<div class="owl-video-play-icon"></div>
<div class="owl-video-tn" style="opacity:1;background-image:url("></div>
var content = '<div class="owl-item" data-video="">'+html+'</div>';
owl.trigger('add.owl.carousel', [$(content), 0]).trigger('refresh.owl.carousel');

I also tried to add them like this:

var content = `<div class="item-video" data-merge="1"><a class="owl-video" href=""></a></div>`;
owl.trigger('add.owl.carousel', [$(content), 0]).trigger('refresh.owl.carousel');

My idea is later on to fetch video URLs from Ghost CMS API and initiate Owl Carousel videos with this data. Help on this highly appreciated!

Answer Source

I got it working by using different approach; dynamically add videos as iframe-embeds.

This is what I got now and everything working. lazyYT is jQuery-plugin for lazy-loading youtube-videos.

var content = `<div data-merge="1" class="item-video"> <div class="lazyYT" data-youtube-id="`+parsedUrl+`" data-ratio="16:9"></div></div>` // Add video to carousel owl.trigger('add.owl.carousel', [$(content), 0]).trigger('refresh.owl.carousel').trigger('to.owl.carousel', 1);

Just wanted to let you know and maybe its useful for somebody. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download