以下是我想要达到的目标。
- 使用CSS transform 转换盒子的Y轴位置,不需要进行过渡。例如:平移Y - 200px
- 用过渡将盒子转换回原始位置。例如:翻译Y - 0px
似乎最后一个翻译正在取消第一个翻译。我只是不完全确定如何正确地链接它。
见下面我的尝试:
$(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>
这应该对您有所帮助。使用show()
在两个动画之间添加一个小超时
$(document).ready(function() {
$( 'body' ).on( 'click', '.box', function() {
$( this ).css( 'transform', 'translate(0px, 200px)' ).show().addClass( 'animating' ).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>
点击我
修改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;
}
希望对你有帮助。
可以对第二个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>
$('.box').click(function() {
$(this).animate({
bottom: "200px"
}, 100, function() {
$(this).addClass( 'animating' );
$(this).animate({
top:"0px"
},2000);
});
} );
.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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="box">Hello</div>
我已经使用jquery animate来执行所需的动画