My issue is I am using Angular 6, Bootstrap v4 and jQuery for the multi-item carousel. If I have data like [1,2,3,4,5,6] there on the carousel it has to show [1,2,3]. After pressing the next it has to show [4,5,6].
Up to this part I achieved, I am facing issues. Only the first active card is moving and there is a lagging also and the previous and next button are not visible. Please check once Stackblitz file you will understand the issue
Here is my work in Stackblitz
https://stackblitz.com/edit/angular-jvr6dh
HTML code
<div class="container">
<div id="dataInfo">
<h2>Information</h2>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner row w-100 mx-auto">
<div class="carousel-item col-md-4 active">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap">
<div class="card-body">
<h4 class="card-title">Card 1</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"><div class="card-body">
<h4 class="card-title">Card 2</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"><div class="card-body">
<h4 class="card-title">Card 3</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"><div class="card-body">
<h4 class="card-title">Card 4</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"> <div class="card-body">
<h4 class="card-title">Card 5</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"><div class="card-body">
<h4 class="card-title">Card 6</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
<div class="carousel-item col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg" width="100" height="100" alt="Card image cap"><div class="card-body">
<h4 class="card-title">Card 7</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
. ts code:
$("#myCarousel").on("slide.bs.carousel", function(e) {
var $e = $(e.relatedTarget);
var idx = $e.index();
var itemsPerSlide = 3;
var totalItems = $(".carousel-item").length;
if (idx >= totalItems - (itemsPerSlide - 1)) {
var it = itemsPerSlide - (totalItems - idx);
for (var i = 0; i < it; i++) {
// append slides to end
if (e.direction == "left") {
$(".carousel-item")
.eq(i)
.appendTo(".carousel-inner");
} else {
$(".carousel-item")
.eq(0)
.appendTo($(this).find(".carousel-inner"));
}
}
}
});
}
.scss code
.carousel-inner .active,
.carousel-inner .active + .carousel-item,
.carousel-inner .active + .carousel-item + .carousel-item {
display: block;
}
.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left),
.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left) + .carousel-item,
.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left) + .carousel-item + .carousel-item {
transition: none;
}
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
position: relative;
transform: translate3d(0, 0, 0);
}
.carousel-inner .active.carousel-item + .carousel-item + .carousel-item + .carousel-item {
position: absolute;
top: 0;
right: -33.3333%;
z-index: -1;
display: block;
visibility: visible;
}
/* left or forward direction */
.active.carousel-item-left + .carousel-item-next.carousel-item-left,
.carousel-item-next.carousel-item-left + .carousel-item,
.carousel-item-next.carousel-item-left + .carousel-item + .carousel-item,
.carousel-item-next.carousel-item-left + .carousel-item + .carousel-item + .carousel-item {
position: relative;
transform: translate3d(-100%, 0, 0);
visibility: visible;
}
/* farthest right hidden item must be abso position for animations */
.carousel-inner .carousel-item-prev.carousel-item-right {
position: absolute;
top: 0;
left: 0;
z-index: -1;
display: block;
visibility: visible;
}
/* right or prev direction */
.active.carousel-item-right + .carousel-item-prev.carousel-item-right,
.carousel-item-prev.carousel-item-right + .carousel-item,
.carousel-item-prev.carousel-item-right + .carousel-item + .carousel-item,
.carousel-item-prev.carousel-item-right + .carousel-item + .carousel-item + .carousel-item {
position: relative;
transform: translate3d(100%, 0, 0);
visibility: visible;
display: block;
visibility: visible;
}
You may find it easier to write your own carousel behaviour, and remove any reliance on jquery. You can still use Bootstrap to style.
We can also use Angular animations to retain the animation behaviour. (Please note that this is the first time I've used Angular animations, so there may be a neater way of doing this)
First step is to create a directive, which handles the
next
/prev
behaviour:You can then access the
prev
andnext
functions from the HTML:Change the
carousel-inner
element, like so:We can then use the
cara
variable to access the public functions:Finally, mark 3 of the
carousel-item
s as.active
, so that 3 are displayed at the same time.Here is a StackBlitz demo
Based on your example code, it looks like simplifying the Bootstrap code itself to use the standard carousel is the best approach. The link below is a fork of the example you provided with the following changes:
https://stackblitz.com/edit/angular-yaevix
You have each card set as a carousel-item but in your description you want to paginate 3 at a time. The correct approach here is to have one carousel-item for every three cards. See example below
Example of one carousel item with multiple cards:
here is simple pure angular directive way implementation for multi item carousel
here you can configure how many items on a slide
i hope you can do a single item carousel too with this
https://github.com/amalroshan/angular-multiitem-carousel
It's a long time of this question, but I can not resist to make another approach.
Disclaimer I stole 4 ideas of the great Netanel Basal and his amazing carousel
Disclamer 2 it's a long, long answer, so to animate to read, here is we are going to get
A carousel is only an html
where the div #carousel move to left/rigth with one animation.
The first idea from Nenatel I borrow is the animation. It's only has two functions: (I use severals variables to parametice the functions)
So, we call to
Well, I´m going to suppose I have 6 different images and we want show 4 at time. There are a two critical positions. Bellow our carousel in different states
So the case a) is when slide=0, the case b) is when slide=2 The case c) is when our slide 0 is in the 4th position and the case d) when slide=6
Clearly, the states c) and d) create us a problem because the
*
show empty espaces (and we don't want whites spaces). So, Why not repeat at first of our images the images 4, 5 and 6 and at the end the images 0 1 and 3, So our states c) and d) becomes likeWell we need take account that the case c) -when slide=-3, it's identical than slide=4
So, if after a movement fall in a slide less than 0, we move instantly to the left
The other case to take account is d) when we click to the right button. In that case we need make two movements
So, our function transitionCarousel becomes like
Well, we need some mechanics to duplicate slides. We are going to do this using a structural directive (2nd stole to Netanel Basal)
See that templateRef and viewContainer it's declared as public, this allow us copy the template of each element
inside another one.
Our carousel becomes like
where, e.g.
I want my carousel scalable, so I need know how many slides can be holder inside the carousel. Of course we can enclose the carousel in a div
And send the number of slides to our carousel, but it's better our carousel make the work for us. So we need know the width of the slides. Here the second last stole to Netanel. The idea of Netanel is to have an .html like
if we make a directive that selected a class like
And defined a ViewChild as
in ngAfterViewInit we can ask about
to get the dimensions
Well, I put in a "timer" because if I'm loading images I can forget about the width of this image.
One steps more. The part more complex is duplicate the slides. Remember that slides.first is our first slide and slides.last our last slide. I repaint all in the function resizeCarousel
I add a image to each side if dont' fit exactly the images
well, the last "borrow idea" that I stole to NetBasal, a hotListener to resize window
And that's all folk, three functions to next, prev and set and "listo el pollo"
Update a better aproach to make the animation is used
private transitionCarousel(time: any, slide: number) {
}
private buildAnimation(time: any,slide:number ) { const animation:number=(slide >= this.slides.length)?1:(slide < 0)?2:0;
}