$(".div1").hide();
$(".div2").offset( $(".div1").offset() ).slideDown();
offset(( 似乎返回了相对于文档的元素的当前连接。好。但是当我尝试使用 offset(value( 来设置元素的绳索时,它会相对于元素的当前位置而不是文档进行。
在示例代码中,我尝试将div2 放置在div1 的相同位置。
法典:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<style>
div{margin-bottom: 2px; height: 70px}
.red{background-color: red}
.blue{background-color: blue}
.green{background-color: green}
.orange{background-color: orange}
.pink{background-color: pink}
.msg{background-color: skyblue; border: 1px solid black; border-radius: 5px; display: none}
</style>
<script type="text/javascript" src="/scripts/jquery-1.6.2.min.js"></script>
<script>
$(function(){
$(".msg").offset( $(".blue").offset() ).slideDown();
});
</script>
<body>
<div class="red"></div>
<div class="blue"></div>
<div class="green"></div>
<div class="orange"></div>
<div class="pink"></div>
<div class="msg">
TEST
</div>
</body>
</html>
问题是因为你首先隐藏了.div1
,从而使它的偏移量无法访问。按如下方式更改您的代码,它将起作用:
$(".div2").offset( $(".div1").offset() ).slideDown();
$(".div1").hide();
我已经在 jsFiddle 中测试了它,但目前无法链接到它,因为他们正在更改主机。