如何解决如何使用JQuery设置多个div的位置动画?
我有['ASHTECH UZ-12','TRIMBLE NETR5']
个元素的列表,并希望为其位置添加动画效果。例如,我可能在容器内有4个div,如下所示:
<div>
现在,假设我想将div移动到列表中索引1处
<div class="container">
<div class="bar" style="height: 70px"> 70 </div>
<div class="bar" style="height: 50px"> 50 </div>
<div class="bar" style="height: 30px"> 30 </div>
<div class="bar" style="height: 10px"> 10 </div>
</div>
到达div在列表中索引0处的位置
<div class="bar" style="height: 50px"> 50 </div>
我会调用该函数
<div class="bar" style="height: 70px"> 70 </div>
问题在于动画完成后,div的顺序会跳动。
我知道该行为的原因来自animateBar(1,0)
行,但是我不确定在此放置什么以防止发生跳跃。我尝试了各种div2.insertBefore(div1)
,但这些似乎都无法解决问题。
出了什么问题,我该如何解决?
before,after,insertBefore,and insertAfter
function animateBar(fromIndex,toIndex) {
const div1 = $(".bar").eq(fromIndex);
const div2 = $(".bar").eq(toIndex);
const distance = div1.offset().left - div2.offset().left;
$.when(
div1.animate({
left: -distance
},1000
)
).done(() => {
div1.css("left","0px");
div2.insertBefore(div1);
});
}
$("button").click(function() {
animateBar(1,0);
animateBar(0,1);
setTimeout(function() {
animateBar(3,2);
animateBar(2,3);
},2000);
setTimeout(function() {
animateBar(2,0);
animateBar(0,1);
animateBar(1,2);
},4000);
});
.container {
width: 100vw;
position: relative;
text-align: center;
}
.bar {
position: relative;
margin: 10px;
background: lightblue;
padding: 5px;
text-align: center;
display: inline-block;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。