jQuery:show() 与 slipDown() - 对 $(document).height() 的不同影响!

发布于 2024-12-29 14:20:38 字数 484 浏览 7 评论 0原文

这很奇怪。我有一个div。它是用 display: none 设置的。

如果我使用 $('#id').show(); ,则 $(document).height() 将更新为 #id 的高度。

但是,如果我使用 $('#id').slideDown(); ,则 $(document).height() 保持不变。

如果您想测试它,请尝试以下操作:

console.log("before",$(document).height());
$('#id').show(); //slideDown();
console.log("after",$(document).height());

请 jQuery Master (jQM) 解释一下吗? :) [顺便说一句,我使用的是 Firefox 9.0.1。]

This is bizarre. I have a div. It is set with display: none.

If I use $('#id').show(); then $(document).height() is updated with the height of #id.

However, if I use $('#id').slideDown(); then $(document).height() remains unchanged.

If you would like to test it, try the following:

console.log("before",$(document).height());
$('#id').show(); //slideDown();
console.log("after",$(document).height());

Could a jQuery Master (jQM) explain this, please? :) [BTW, I am using Firefox 9.0.1.]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2025-01-05 14:20:39

在测量高度之前,您必须等待 slideDown() 动画完成。对 slideDown() 的函数调用立即完成(此时我假设您测量了文档高度),但动画才刚刚开始,因此该项目几乎没有(如果有的话)甚至改变高度。 SlideDown 方法所做的只是启动动画(然后随着时间的推移在计时器上运行至完成)。因此,您可能要做的是在动画开始后、在动画完成之前很久就测量高度。

如果您想在 slideDown() 动画完成后测量文档高度,那么您需要通过完成回调函数(动画最终完成时发出信号)来执行此操作,如下所示:

console.log("before",$(document).height());
$('#id').slideDown(function() {
    console.log("after",$(document).height());
});

You will have to wait for the completion of the slideDown() animation before measuring the height. The function call to slideDown() finishes immediately (at which point I assume you measure the document height), but the animation has only just begun so the item has barely, if at all, even changed height yet. All the slideDown method does is start the animation (which then runs to completion on timers over time). So, what you're probably doing is measuring the height right after the animation is started, long before it's done.

If you want to measure the document height after the slideDown() animation finishes, then you need to do so from a completion callback function (which signals when the animation is finally done) like this:

console.log("before",$(document).height());
$('#id').slideDown(function() {
    console.log("after",$(document).height());
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文