Reputation: 4168
I'm trying to animate Bootstrap Carousel
with animate.css
I finished with caption animation
everything works, but how to animate an item
HTML:
<div class="item zoomInLeft animated">
<img data-src="/images/1.jpg" alt="...">
<div class="carousel-caption">
<h1 data-animation="bounceInDown" data-delay="400">Lorem Ipsum</h1>
<h3 data-animation="bounceInDown" data-delay="600">Lorem Ipsum</h3>
</div>
</div>
My code for captions if anybody needs (JS)
function carousel_init(c){
var self = this;
self.carouselAnimations = function (elems) {
elems.each(function (index) {
var el = $(this);
var animEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
var animation = el.data('animation')+' animated';
var delay = el.data('delay');
el.attr('style', '-webkit-animation-delay:'+delay+'ms; -moz-animation-delay:'+delay+'ms; -o-animation-delay:'+delay+'ms; animation-delay:'+delay+'ms')
.promise().done(function(){
el.addClass(animation).one(animEnd, function () {
el.removeClass(animation);
});
});
});
}
var carousel = $(c);
var firstElems = carousel.find('.item:first').find('[data-animation]');
carousel.carousel({
interval: 4000,
pause:false,
wrap:true
});
self.carouselAnimations(carousel, firstElems);
carousel.on('slide.bs.carousel', function (e) {
var elems = $(e.relatedTarget).find('[data-animation]');
self.carouselAnimations(elems);
});
}
So I am goin throu all elements in active.item
with data-animation
(plus data-delay) and animating them.
- It is possible also to
animate an Item
of bootstrap carousel?
if I just add class zoomOutLeft animated
works but before that I need to add zoomInLeft animated
(for example)
Thank you in adnvace
Updated Question: @makshh Suggestion
.home-carousel .carousel-inner > .item {
-webkit-transition: 0.6s ease-in-out left;
transition: 0.6s ease-in-out left;
}
@media all and (transform-3d), (-webkit-transform-3d) {
.home-carousel .carousel-inner > .item {
-webkit-transition: -webkit-transform 0.6s ease-in-out;
transition: transform 0.6s ease-in-out;
}
}
@media all and (transform-3d), (-webkit-transform-3d) {
.home-carousel .carousel-inner> .item.next,
.home-carousel .carousel-inner> .item.active.right {
-webkit-transform: translate3d(100%, 0, 0) scale(0.5) rotate(8deg);
transform: translate3d(100%, 0, 0) scale(0.5) rotate(8deg);
}
.home-carousel .carousel-inner> .item.prev,
.home-carousel .carousel-inner> .item.active.left {
-webkit-transform: translate3d(-100%, 0, 0) scale(0.5) rotate(-8deg);
transform: translate3d(-100%, 0, 0) scale(0.5) rotate(-8deg);
}
.home-carousel .carousel-inner> .item.next.left,
.home-carousel .carousel-inner> .item.prev.right,
.home-carousel .carousel-inner> .item.active {
-webkit-transform: translate3d(0, 0, 0) scale(1) rotate(0);
transform: translate3d(0, 0, 0) scale(1) rotate(0);
}
}
works but only in Chrome it is possible to make it work cross-browser becuase as I know transform3d works even in IE10+
Update: Removing @media all and (transform-3d), (-webkit-transform-3d)
makes it work in IE10+ (FF, Opera)
But the question is still here can we integrate animate.css + bootstrap carousel.
Upvotes: 3
Views: 8992
Reputation: 8667
HTML (nothing special, pure Bootstrap):
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
<li data-target="#carousel-example-generic" data-slide-to="4"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/f/fa/No._23_Post_(Skelmorlie)_Image_3.jpg/640px-No._23_Post_(Skelmorlie)_Image_3.jpg" alt="...">
</div>
<div class="item">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Image-Lomnicky_stit_from_Slavkovsky_stit_2.jpg/640px-Image-Lomnicky_stit_from_Slavkovsky_stit_2.jpg" alt="...">
</div>
<div class="item">
<img src="https://upload.wikimedia.org/wikipedia/commons/1/19/Image-Peacock_Springs_Entrance.jpg" alt="...">
</div>
<div class="item">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Image-of-Sumadija-2.jpg/640px-Image-of-Sumadija-2.jpg" alt="...">
</div>
<div class="item">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Image-Great_Swamp_National_Wildlife_Refuge_New_Jersey03.jpg/640px-Image-Great_Swamp_National_Wildlife_Refuge_New_Jersey03.jpg" alt="...">
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
</div>
CSS (disabled transitions, transforms, and set left to 0, I also changed animation-duration to match Bootstrap carousel default duration, if you want different value than 0.6s you have to overwrite Carousel.TRANSITION_DURATION = 600
in bootstrap.js
file and change animation-duration
to yours):
.item {
transition: none !important;
left: 0 !important;
transform: none !important;
}
.animated {
animation-duration: .6s;
}
JS (I didn't test it and I'm pretty sure it can be improved significantly):
$('#carousel-example-generic').on('slide.bs.carousel', function() {
if ($('.carousel-inner .item:last').hasClass('active')) {
$('.carousel-inner .item:first').addClass('animated zoomInDown');
} else {
$('.item.active').next().addClass('animated zoomInDown');
}
$('.item.active').addClass('animated zoomOutDown');
});
$('#carousel-example-generic').on('slid.bs.carousel', function() {
$('.item').removeClass('animated zoomInDown zoomOutDown')
});
$('.left').click(function () {
if ($('.carousel-inner .item:first').hasClass('active')) {
$('.carousel-inner .item:last').addClass('animated zoomInDown');
} else {
$('.item.active').prev().addClass('animated zoomInDown');
}
});
$('.carousel-indicators > li').click(function () {
$('.item').addClass('animated zoomInDown');
});
Upvotes: 5