Combination of animation and transition not working properly

后端 未结 2 1051
小蘑菇
小蘑菇 2020-11-30 12:41

I have been trying to put some basic CSS3 animation. The objective is to toggle a class on the click event of a button and animate a div based on the added class. The code w

2条回答
  •  抹茶落季
    2020-11-30 13:21

    It is an expected behavior. You need to use two separate animations or stick with the transition only.

    A transition is an animation, just one that is performed between two distinct states - i.e. a start state and an end state. Like a drawer menu, the start state could be open and the end state could be closed, or vice versa.

    If you want to perform something that does not specifically involve a start state and an end state, or you need more fine grain control over the keyframes in a transition, then you've got to use an animation.

    This is the way to animate the element with transition:

    $('button').click(function() {
      $('div').toggleClass('clicked');
    });
    div {
      background-color: #ccc;
      height: 100px;
      width: 100px;
      transition-property: top, left;
      transition-duration: 1s;
      transition-timing-function: linear;
      position: relative;
      top: 0;
      left: 0;
    }
    .clicked {
      top: 100px;
      left: 100px;
    }
    
    
    

提交回复
热议问题