简体   繁体   English

jQuery动画-获取元素的未来位置

[英]jQuery animate - get future position of element

I'm having trouble getting the position of an element because my animation is long and $(this).position().top is calculated too early. 我无法获得元素的位置,因为我的动画很长,并且$(this).position()。top计算得太早了。

How can I get a future position value of an element before it animates to that position? 如何在元素动画化到该位置之前获得它的未来位置值?

That's not what you want. 那不是你想要的。 You want to get the position of the element AFTER the animation has completed. 您希望在动画完成后获取元素的位置。 You need to add a callback function to your animation, and call the position from inside that callback function. 您需要在动画中添加回调函数,然后从该回调函数内部调用位置。 Here you go -> 你去->

$("whatever").animate({
  //do stuff for animation
}, 'delay', function(){
  //our animation has completed, this is our callback function.
  //we can now successfully get our position.
  $(this).position().top
});

Hope this helps! 希望这可以帮助!

  • Also

Please consider improving your accept rating, 20% either means that your questions are poorly formatted, or you aren't following the community guidelines. 请考虑提高接受度,将其设为20%要么表示您的问题格式不正确,要么不遵循社区准则。 Please read the FAQ to understand interacting with Stackoverflow.com 请阅读常见问题解答以了解与Stackoverflow.com的交互

https://stackoverflow.com/faq https://stackoverflow.com/faq

  • Side note: 边注:

You will also get the badge 'Analytical' for reading the entire FAQ. 您还将获得徽章“ Analytical”,以阅读完整的常见问题解答。 I highly suggest it. 我强烈建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM