带有ng-boostrap和Angular 2的旋转木马的动画

时间:2017-03-02 23:24:33

标签: javascript twitter-bootstrap angular ng-bootstrap

我正在使用带有ng-bootstrap的轮播组件。我理解正确的动画功能是一个开放的问题,它正好适用于角度2组件生命周期(Github issue)。

我的问题:有没有办法使用CSS作为动画的解决方法?

我已经设置了一个plunker,它对于旋转木马有效,但不会淡出。

.carousel-item.active{

    -webkit-animation: fadein 1.4s; 
       -moz-animation: fadein 1.4s; 
        -ms-animation: fadein 1.4s; 
         -o-animation: fadein 1.4s; 
            animation: fadein 1.4s;

}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein { 
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

有没有办法让淡出工作?我尝试过渡但失败了。

5 个答案:

答案 0 :(得分:5)

好的,回答我自己的问题。以下CSS hack将使动画正常工作

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;
}

工作Plunker

答案 1 :(得分:2)

我设法根据您的方法创建从左到右的幻灯片动画。当活动幻灯片丢失.active然后新的.active幻灯片放入延迟动画时,活动幻灯片会向右转移。

虽然我建议使用ngx-swiper-wrapper代替 - 我到目前为止找到的最佳角度旋转木马(https://idangero.us/swiper/

&#13;
&#13;
.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;
  }
}
&#13;
&#13;
&#13;

答案 2 :(得分:2)

我想要幻灯片之间的淡入/淡出过渡,并找到了一个更简单的解决方案:

::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
  }
}

如果您不想使用:: ng-deep(似乎已被弃用,或者至少我上周在angular.io中阅读过),则可以使用全局样式文件接触所有组件中的所有班级

答案 3 :(得分:0)

调整@ user3682091答案后使其在角度7中工作  希望对别人有帮助

这是我的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>

这是我的style.css文件(全局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;
}

答案 4 :(得分:0)

这是我在Angular 9,ng-bootstrap版本6.0.0中使用的两个动画。最简单的方法是为.carousel-item.active提供入口动画。动画取自animista.net/play/entrances。只要确保将css添加到全局styles.css

动画1

Animation 1

.carousel-item.active {
    -webkit-animation: flip-in-ver-left 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
            animation: flip-in-ver-left 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}

 @-webkit-keyframes flip-in-ver-left {
    0% {
      -webkit-transform: rotateY(80deg);
              transform: rotateY(80deg);
      opacity: 0;
    }
    100% {
      -webkit-transform: rotateY(0);
              transform: rotateY(0);
      opacity: 1;
    }
  }
  @keyframes flip-in-ver-left {
    0% {
      -webkit-transform: rotateY(80deg);
              transform: rotateY(80deg);
      opacity: 0;
    }
    100% {
      -webkit-transform: rotateY(0);
              transform: rotateY(0);
      opacity: 1;
    }
  }

动画2

Animation 2

.carousel-item.active {
    -webkit-animation: tilt-in-fwd-tr 0.6s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
            animation: tilt-in-fwd-tr 0.6s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}

 @-webkit-keyframes tilt-in-fwd-tr {
    0% {
      -webkit-transform: rotateY(20deg) rotateX(35deg) translate(300px, -300px) skew(-35deg, 10deg);
              transform: rotateY(20deg) rotateX(35deg) translate(300px, -300px) skew(-35deg, 10deg);
      opacity: 0;
    }
    100% {
      -webkit-transform: rotateY(0) rotateX(0deg) translate(0, 0) skew(0deg, 0deg);
              transform: rotateY(0) rotateX(0deg) translate(0, 0) skew(0deg, 0deg);
      opacity: 1;
    }
  }
  @keyframes tilt-in-fwd-tr {
    0% {
      -webkit-transform: rotateY(20deg) rotateX(35deg) translate(300px, -300px) skew(-35deg, 10deg);
              transform: rotateY(20deg) rotateX(35deg) translate(300px, -300px) skew(-35deg, 10deg);
      opacity: 0;
    }
    100% {
      -webkit-transform: rotateY(0) rotateX(0deg) translate(0, 0) skew(0deg, 0deg);
              transform: rotateY(0) rotateX(0deg) translate(0, 0) skew(0deg, 0deg);
      opacity: 1;
    }
  }