gpt4 book ai didi

javascript - 如何将一个
移到另一个
前面,然后再移回来?

转载 作者:行者123 更新时间:2023-11-30 10:29:23 24 4
gpt4 key购买 nike

我有两个 div。一个在前面,我想让后面的那个向右移动,然后回到第一个上面。我使用了 jQuery,但它会立即更改 z-index,然后继续将一个 div 向右移动并向左移动到其原始位置。这就是我尝试这样做的方式:

<!DOCTYPE html>
<html>
<head>
<style>
.block {
position: absolute;
background-color: #abc;
left: 0px;
top:30px;
width: 60px;
height: 60px;
margin: 5px;
}

.block1 {
position: absolute;
background-color: red;
left: 0px;
top:30px;
width: 60px;
height: 60px;
margin: 5px;
z-index: 999;
}
</style>
<script src="js/jquery-1.10.2.min.js"></script>
</head>
<body>

<div class="block" onmouseout="hide();"></div>
<div class="block1" onmouseover="show();"></div>

<script>
function show() {
$(".block").animate({left: '+=100px'}, 2000);
$(".block1").css('zIndex', '-10000');
$(".block").animate({left: '-=100px'}, 2000);
};

function hide() {
$(".block").animate({left: '+=100px'}, 2000);
$(".block1").css('zIndex', '10000');
$(".block").animate({left: '-=100px'}, 2000);
};
</script>

</body>
</html>

最佳答案

animate 方法是异步的。在完成第一个动画后,您必须通过向 animate 方法提供回调函数来更改 z-index:

function show() {
$(".block").animate({left: '+=100px'}, 2000, function() {
$(".block1").css('zIndex', '-10000');
$(".block").animate({left: '-=100px'}, 2000);
});
};

来自 .animate() 的文档:

.animate( properties [, duration ] [, easing ] <b>[, complete ]</b> )

complete
Type: Function()
A function to call once the animation is complete.

关于javascript - 如何将一个 <div> 移到另一个 <div> 前面,然后再移回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17859259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com