动态生成新内容时如何更改偏移值?
当页面加载时,我们使用 offset() 方法获取输入框的偏移值。但是,在该输入框上方,有一个按钮,单击时会显示隐藏的 div(并且该 div 可以根据动态内容具有不同的高度)。单击该按钮后,偏移值保持不变,但现在文档的高度增加了。我们希望当文档高度改变时偏移值也动态改变。
这可能吗?如果是这样,怎么办?
We are getting the offset value of an input box when the page loads using the offset() method. However, above that input box, there's a button that shows a hidden div when clicked (and this div can have varying heights depending on the dynamic content). After that button is clicked, the offset value remains the same but now the height of the document has increased. What we want to happen is for the offset value to also dynamically change when the document height changes.
Is this possible? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能 100% 确定是否可以根据您提供的信息确定您遇到的问题。如果我们可以看到一些代码或有问题页面的链接,那么提供准确的解决方案会更容易。
听起来您正在使用 jQuery 的 offset 方法来确定页面加载时输入框的位置。听起来您正在存储该值,但这不是您想要的。对 offset 的调用只会提供调用时元素的位置。
相反,像这样缓存选择器
并在每次需要当前位置时调用它的方法
I'm not 100% sure if I can identify the problem you're having from the information you provided. If we could see some code or have a link to the page with the issue, it would be easier to provide an accurate solution.
It sounds like you're using jQuery's offset method to determine the location of an input box on page load. It also sounds like you're storing that value, which is not what you want. A call to offset will only provide the position of the element at the time of the call.
Instead, cache the selector like this
And call the method on it every time you want the current position
.offset()
是动态的。您没有将值存储在变量中,是吗?只需直接访问$(element).offset().top
,您将始终获得正确的值。jsFiddle: http://jsfiddle.net/we5FP/
.offset()
is dynamic. You're not storing the value in a variable are you? Just access$(element).offset().top
directly, and you'll always get the correct value.jsFiddle: http://jsfiddle.net/we5FP/