I have a problem with displaying dynamic content with Owl carousel 2 using JSON/AJAX. I get no error messages in console, but cant get the carousel to work. I only see a blank page. I am able to append the image url's fetched from JSON file with jquery.append, but they wont be shown in the carousel that way. Displays are set to "block". Any tips what am i missing?
index.html -
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rengastie</title>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="row">
<div class="small-12 columns">
<div id="top-carousel" class="owl-carousel owl-theme">
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
app.js -
$(document).ready(function() {
$('.owl-carousel').owlCarousel();
});
var $owl = $('.owl-carousel');
$owl.owlCarousel({
loop:true,
items: 1,
autoplay:true,
autoplayTimeout:3000,
autoplayHoverPause:false
});
$.ajax({
url: 'json/data.json',
dataType: 'json',
success: function(data) {
var content = '';
var alt = "pic123";
for (i in data.owl) {
content += "<div class='item'><img src=\"" +data.owl[i].img+ "\" alt=\"" +alt+ "\"></div>";
}
$owl.trigger('insertContent.owl',content);
//$owl.append(content); This stacks the images above eachother, not affected by the carousel settings
}
});
data.json -
{
"owl" : [
{
"img": "img/kuvaIso1.jpg"
},
{
"img": "img/kuvaIso2.jpg"
},
{
"img": "img/kuvaIso3.jpg"
},
{
"img": "img/kuvaIso4.jpg"
},
{
"img": "img/kuvaIso5.jpg"
}
]
}