jQuery:show() 与 slipDown() - 对 $(document).height() 的不同影响!
这很奇怪。我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在测量高度之前,您必须等待
slideDown()
动画完成。对slideDown()
的函数调用立即完成(此时我假设您测量了文档高度),但动画才刚刚开始,因此该项目几乎没有(如果有的话)甚至改变高度。 SlideDown 方法所做的只是启动动画(然后随着时间的推移在计时器上运行至完成)。因此,您可能要做的是在动画开始后、在动画完成之前很久就测量高度。如果您想在
slideDown()
动画完成后测量文档高度,那么您需要通过完成回调函数(动画最终完成时发出信号)来执行此操作,如下所示:You will have to wait for the completion of the
slideDown()
animation before measuring the height. The function call toslideDown()
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: