Animation for carousel with ng-boostrap and Angular 2

核能气质少年 提交于 2019-11-29 07:55:26

Alright, answering my own question. The following CSS hack will make the animation work just fine

ngb-carousel {
    width: inherit;
    height: inherit;
}


.carousel-inner {
    overflow: visible;
}

.carousel-item {
    display: flex !important;
    opacity: 0;
    visibility: hidden;
    transition: opacity 1.2s ease-in-out, visibility 1.2s;
    z-index: -1;
    position: absolute;
}

.carousel-item.active{
    opacity: 1;
    visibility: visible;
    z-index: 10;
}

.carousel-control-prev {
     z-index: 20;
}


.carousel-control-next {
     z-index: 20;
}

.carousel-indicators{
    z-index: 20;
}

Working Plunker

I wanted a fade in/fade out transition between slides and found a simpler solution to this:

::ng-deep {
  .carousel-item {
    transition: opacity 0.7s ease!important
    position: absolute!important
    display: block!important
    opacity: 0
 }

 .carousel-item.active {
    position: relative!important
    opacity: 1
  }
}

If you don't want to use ::ng-deep (it seems that is going to be deprecated, or at least I read so last week in angular.io) you can use a global styles file that will reach all classes in all components

I managed to create left-to-right slide animation based on your approach. active slide goes out with transition to the right when it loses .active and then new .active slides in with delayed animation.

though I recommend using ngx-swiper-wrapper instead - best angular carousel I found so far (https://idangero.us/swiper/)

.carousel-inner {
  width: 640px;
  height: 240px;
}

.carousel-item {
  padding: 40px 55px;
  opacity: 0;
  transition: opacity .3s ease-in-out;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  display: block !important;
}

.carousel-item.active {
  z-index: 1;
  opacity: 1;
  transition-delay: .3s;
  transform: none;
  animation: slideFromLeft .3s;
  animation-delay: .3s;
}

@keyframes slideFromLeft {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: none;
  }
}

Got it to work in angular 7 after tweaking @user3682091 answer Hope it helps someone else

Here's my html

<div class="section" id="carousel">
  <div class="container">
    <div class="title">
      <h4>Carousel</h4>
    </div>
    <div class="row justify-content-center" style="height: 50vw;">
      <div class="col-12" style="height: 100%; width:100%">
        <ngb-carousel>
          <ng-template ngbSlide>
            <img class="d-block" src="assets/img/bg1.jpg" alt="First slide">
            <div class="carousel-caption d-none d-md-block">
              <h5>Nature, United States</h5>
            </div>
          </ng-template>
          <ng-template ngbSlide>
            <img class="d-block" src="assets/img/bg3.jpg" alt="Second slide">
            <div class="carousel-caption d-none d-md-block">
              <h5>Somewhere Beyond, United States</h5>
            </div>
          </ng-template>
          <ng-template ngbSlide>
            <img class="d-block" src="assets/img/bg4.jpg" alt="Third slide">
            <div class="carousel-caption d-none d-md-block">
              <h5>Yellowstone National Park, United States</h5>
            </div>
          </ng-template>
        </ngb-carousel>
      </div>
    </div>
  </div>
</div>

Here's my style.css file(global css)

ngb-carousel {
  //   width: inherit;
  //   height: inherit;
  width: 100%;
  height: 100%;
}


.carousel-inner {
  overflow: visible;
}

.carousel-item {
  display: flex !important;
  opacity: 0;
  visibility: hidden;
  transition: opacity 1.2s ease-in-out, visibility 1.2s;
  z-index: -1;
  position: absolute;
}

.carousel-item.active {
  opacity: 1;
  visibility: visible;
  transition: opacity 1.2s ease-in-out, visibility 1.2s;
  z-index: 10;
}

.carousel-control-prev {
  z-index: 20;
}


.carousel-control-next {
  z-index: 20;
}

.carousel-indicators {
  z-index: 20;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!