I'm trying to implement Slider Syncing using slick
library. I have an array of images called pictures
coming from the backend. I loop over these images to populate them under my slider-for
and slider-nav
divs.
HTML code:
<head>
<style>
.slick-arrow {
color: #666666;
background: red;
}
.slider-for {
max-width: 960px;
margin-left: auto;
margin-right: auto;
margin-top: 5%;
}
.slider-nav h1 {
display: inline-block;
}
.slider-nav .slick-slide {
text-align: center;
width: 337px;
}
.container {
margin-bottom: 5%;
margin-top: 5%;
}
</style>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.css"/>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick-theme.css"/>
</head>
<div class="container-fluid padding25">
<h2>Pictures</h2>
<div class="slider-for">
<% var index = 0;%>
<%_.each(pictures, function(picture) { %>
<div>
<img src="<%=picture.pictureUrl%>" class="img-rounded" width="460" height="345"/>
</div>
<%
index++;
});
%>
</div>
<div class="slider-nav">
<%_.each(pictures, function(picture) { %>
<div>
<img src="<%=picture.pictureUrl%>" class="img-rounded" width="150" height="300"/>
</div>
<%
index++;
});
%>
</div>
<div class="row">
<h2>Videos</h2>
...
</div>
</div>
javaScript:
<script type="text/javascript" src="/slick-1.5.7/slick/slick.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
console.log("inside docready");
$('.slider-for').slick({
slidesToShow: 1,
slidesToScroll: 1,
fade: true,
asNavFor: '.slider-nav',
});
$('.slider-nav').slick({
slidesToShow: 3,
slidesToScroll: 1,
dots: true,
asNavFor: '.slider-for',
dots: true,
centerMode: true,
centerPadding: '3%',
focusOnSelect: true,
});
$('.single-item').slick({
dots: false,
arrows: true,
});
});
</script>
Only one image is visible in the slider-nav
on page load. On clicking left/right slick-arrow
, the images appear properly. I tried to write the same code in jsFiddle. But I think it works well there (Not sure if its because I have refrained from using for
loop there.)
slider-nav
images to load properly. (without clicking any slick-arrows) – Connel