如何使用jQuery' s .css()触发转换的连续CSS转换?

时间:2016-10-06 05:38:33

标签: jquery html css css-transitions translate

这是我想要实现的目标的细分。

  1. 使用CSS转换转换框的Y位置,不带转换。例如翻译Y - 200px。
  2. 通过转换将框翻译回原始位置。例如翻译Y - 0px。
  3. 似乎最后一个翻译是取消第一个翻译。我还不完全确定如何正确链接。

    请参阅下面的尝试:

    
    
    $(document).ready(function() {
      $( 'body' ).on( 'click', '.box', function() {
        $( this ).css( 'transform', 'translate(0px, 200px)' );
        $( this ).addClass( 'animating' );
        $( this ).css( 'transform', 'translate(0px, 0px)' );
      } );
    } );
    
    .box {
      transform: translate(0px,0px);
      width: 50px;
      height: 50px;
      display: block;
      background: #0f0;
    }
    
    .box.animating {
      transition: all 2s ease-in-out;
      -webkit-transition: all 2s ease-in-out;
      -moz-transition: all 2s ease-in-out;
      -o-transition: all 2s ease-in-out;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="box">Hello</div>
    &#13;
    &#13;
    &#13;

4 个答案:

答案 0 :(得分:1)

这应该对你有所帮助。使用Python 2在两个动画之间添加一个小超时

&#13;
&#13;
show()
&#13;
$(document).ready(function() {
  $( 'body' ).on( 'click', '.box', function() {
    $( this ).css( 'transform', 'translate(0px, 200px)' ).show().addClass( 'animating' ).css( 'transform', 'translate(0px, 0px)' );
  } );
} );
&#13;
.box {
  transform: translate(0px,0px);
  width: 50px;
  height: 50px;
  display: block;
  background: #0f0;
}

.box.animating {
  transition: all 2s ease-in-out;
  -webkit-transition: all 2s ease-in-out;
  -moz-transition: all 2s ease-in-out;
  -o-transition: all 2s ease-in-out;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请在Click Me

找到您的解决方案

只需更改css:

.box {
  transform: translate(0px,0px);
  width: 50px;
  height: 50px;
  display: block;
  background: #0f0;
}

.box:hover {
   width: 200px;
  height: 200px;
  transition: all 2s ease-in-out;
  -webkit-transition: all 2s ease-in-out;
  -moz-transition: all 2s ease-in-out;
  -o-transition: all 2s ease-in-out;
}

我希望这会对你有所帮助。

答案 2 :(得分:0)

您可以在第二个css转换中使用setTimeOut,如

$(document).ready(function() {
  $( 'body' ).on( 'click', '.box', function() {
     $( '.box').css( 'transform', 'translate(0px, 200px)' ); 
     setTimeout(function(){
        $( '.box' ).addClass( 'animating' );
        $( '.box' ).css( 'transform', 'translate(0px, 0px)' );
     }, 200);
   
  });
});
.box {
  transform: translate(0px,0px);
  width: 50px;
  height: 50px;
  display: block;
  background: #0f0;
}

.box.animating {
  transition: all 2s ease-in-out;
  -webkit-transition: all 2s ease-in-out;
  -moz-transition: all 2s ease-in-out;
  -o-transition: all 2s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">Hello</div>

答案 3 :(得分:0)

&#13;
&#13;
$('.box').click(function() {
    $(this).animate({
            bottom: "200px"
             }, 100, function() {
             $(this).addClass( 'animating' );
             $(this).animate({
               top:"0px"
             },2000);
          });
  } );
&#13;
.box {
  transform: translate(0px,0px);
  width: 50px;
  height: 50px;
  display: block;
  background: #0f0;
  position:absolute;
}

.box.animating {
  transition: all 2s ease-in-out;
  -webkit-transition: all 2s ease-in-out;
  -moz-transition: all 2s ease-in-out;
  -o-transition: all 2s ease-in-out;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="box">Hello</div>
&#13;
&#13;
&#13;

我使用jquery animate来执行所需的动画