使用@keyframes的复杂CSS动画

时间:2019-07-18 18:43:29

标签: javascript jquery html css css-animations

嗨,我需要创建像https://d.pr/v/P8aLvl这样的按钮交换动画

这是通过使用一些js和CSS属性opacityscalebox-shadow来完成的。

我尝试创建它,但是我的动画看起来不太流畅。参见https://d.pr/v/QLsLdo

查看Button 1Button 2如何闪烁。我不明白我在这里想念什么。

使用的代码如下:

谢谢!

jQuery(function() {

        var container = $(this);

        // console.log(container);

        var checkboxes = $('#button-switch');

        var checkedBoxes = $('#button-switch:checked');


        checkboxes.on('click', function(e) {

          if ( $('#button-switch:checked').length > 0 ) {

            $('.activity-buttons').addClass('selected-buttons first-animation').removeClass('second-animation');

          } else if ( $('#button-switch:checked').length === 0 ) {
            $('.activity-buttons').removeClass('selected-buttons first-animation').addClass('second-animation');
          }

        });
      
    });
.activity-buttons .btn-list {
  padding: 40px 20px;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.activity-buttons .btn-list li {
  position: relative;
  width: 200px;
  height: 40px;
}

.activity-buttons .btn-list .btn {
  position: absolute;
}

.activity-buttons .btn-list .btn.activity-selected {
  opacity: 0;
  pointer-events: none;
}

.activity-buttons.selected-buttons .btn-list .btn.activity-all {
  opacity: 0;
  pointer-events: none;
}

.activity-buttons.selected-buttons .btn-list .btn.activity-selected {
  opacity: 1;
  pointer-events: auto;
}

.activity-buttons.selected-buttons.first-animation .btn-list li .btn.activity-all {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1);
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1);
  -webkit-animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1);
  animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1);
}

.activity-buttons.selected-buttons.first-animation .btn-list li .btn.activity-selected {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .3s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .3s;
  -webkit-animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .3s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .3s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .3s;
  animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .3s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .3s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .3s;
}

.activity-buttons.selected-buttons.first-animation .btn-list li+li .btn.activity-all {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .1s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .1s;
  -webkit-animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1) .1s;
  animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1) .1s;
}

.activity-buttons.selected-buttons.first-animation .btn-list li+li .btn.activity-selected {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .4s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .4s;
  -webkit-animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .4s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .4s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .4s;
  animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .4s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .4s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .4s;
}

.activity-buttons.second-animation .btn-list li .btn.activity-all {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .3s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .3s;
  -webkit-animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .3s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .3s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, hideButtonShadow .19s cubic-bezier(.215, .61, .355, 1) .75s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .3s, buttonScaleLow .31s cubic-bezier(.55, .055, .675, .19) .65s;
  animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .3s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .3s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, hideButtonShadow .19s cubic-bezier(.215, .61, .355, 1) .75s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .3s, buttonScaleLow .31s cubic-bezier(.55, .055, .675, .19) .65s;
}

.activity-buttons.second-animation .btn-list li .btn.activity-selected {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1);
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1);
  -webkit-animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1);
  animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1);
}

.activity-buttons.second-animation .btn-list li+li .btn.activity-all {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .4s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .4s;
  -webkit-animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .4s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .4s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, hideButtonShadow .19s cubic-bezier(.215, .61, .355, 1) .85s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .4s, buttonScaleLow .31s cubic-bezier(.55, .055, .675, .19) .75s;
  animation: showButtonShadow .3s cubic-bezier(.19, 1, .22, 1) .4s, changeBg .29s cubic-bezier(.645, .045, .355, 1) .4s, hideButtonBorder .2s cubic-bezier(.215, .61, .355, 1) .3s, hideButtonShadow .19s cubic-bezier(.215, .61, .355, 1) .85s, buttonScaleHigh .31s cubic-bezier(.645, .045, .355, 1) .4s, buttonScaleLow .31s cubic-bezier(.55, .055, .675, .19) .75s;
}

.activity-buttons.second-animation .btn-list li+li .btn.activity-selected {
  -webkit-transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .1s;
  transition: opacity .2s cubic-bezier(.645, .045, .355, 1) .1s;
  -webkit-animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1) .1s;
  animation: hideButtonScale .2s cubic-bezier(.645, .045, .355, 1) .1s;
}

@-webkit-keyframes hideButtonScale {
  from {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  to {
    -webkit-transform: scale(.5);
    transform: scale(.5);
  }
}

@keyframes hideButtonScale {
  from {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  to {
    -webkit-transform: scale(.5);
    transform: scale(.5);
  }
}

@-webkit-keyframes showButtonScale {
  from {
    -webkit-transform: scale(.5);
    transform: scale(.5);
  }
  to {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@keyframes showButtonScale {
  from {
    -webkit-transform: scale(.5);
    transform: scale(.5);
  }
  to {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@-webkit-keyframes buttonScaleHigh {
  from {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  to {
    -webkit-transform: scale(1.05);
    transform: scale(1.05);
  }
}

@keyframes buttonScaleHigh {
  from {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  to {
    -webkit-transform: scale(1.05);
    transform: scale(1.05);
  }
}

@-webkit-keyframes buttonScaleLow {
  from {
    -webkit-transform: scale(1.05);
    transform: scale(1.05);
  }
  to {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@keyframes buttonScaleLow {
  from {
    -webkit-transform: scale(1.05);
    transform: scale(1.05);
  }
  to {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@keyframes showButtonScale {
  from {
    background: transparent;
  }
  to {
    background: #fff;
  }
}

@-webkit-keyframes showButtonShadow {
  from {
    box-shadow: none;
  }
  to {
    box-shadow: 0 8px 16px rgba(208, 201, 214, .5);
  }
}

@keyframes showButtonShadow {
  from {
    box-shadow: none;
  }
  to {
    box-shadow: 0 8px 16px rgba(208, 201, 214, .5);
  }
}

@-webkit-keyframes hideButtonShadow {
  from {
    box-shadow: 0 8px 16px rgba(208, 201, 214, .5);
  }
  to {
    box-shadow: none;
  }
}

@keyframes hideButtonShadow {
  from {
    box-shadow: 0 8px 16px rgba(208, 201, 214, .5);
  }
  to {
    box-shadow: none;
  }
}

@-webkit-keyframes hideButtonBorder {
  from {
    border-color: #eaeaea;
  }
  to {
    border-color: transparent;
  }
}

@keyframes hideButtonBorder {
  from {
    border-color: #eaeaea;
  }
  to {
    border-color: transparent;
  }
}

@-webkit-keyframes showButtonBorder {
  from {
    border-color: transparent;
  }
  to {
    border-color: #eaeaea;
  }
}

@keyframes showButtonBorder {
  from {
    border-color: transparent;
  }
  to {
    border-color: #eaeaea;
  }
}

@-webkit-keyframes opacityHigh {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes opacityHigh {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="activity-buttons">
  <ul class="btn-list justify-content-center">
    <li>
      <button type="button" class="btn btn-outline-secondary left-align-icon activity-all" data-toggle="modal" data-target="#deleteModal">Button 1</button>
      <button type="button" class="btn btn-outline-secondary left-align-icon activity-selected" data-toggle="modal" data-target="#deleteModal"><i class="icon-delete_outline"></i> Delete selected</button>
    </li>
    <li>
      <button class="btn btn-outline-secondary left-align-icon activity-all">Button 2</button>
      <button class="btn btn-outline-secondary left-align-icon activity-selected"><i class="icon-vertical_align_top"></i> Export selected</button>
    </li>
  </ul>
</div>

<input type="checkbox" id="button-switch" class="ml-5">

1 个答案:

答案 0 :(得分:1)

它可能会抖动,因为它会返回到其原始位置。 尝试animation-fill-mode: forwards; 当动画完成时,这将使元素保持相同状态,而不是还原为原始状态。