简体   繁体   English

如何获取offsetParent div的顶部和左侧属性?

[英]how to get top and left properties of offsetParent div?

As we can get offset top of an element by using like this: 因为我们可以通过使用这样的方式获得元素的顶部偏移:

$(selector).offset().top;

But how can we get the top value of parent offset div? 但是我们怎样才能获得父偏移div的最高值?

I've tried like this but doesn't work? 我试过这样但不起作用?

$(selector).offsetParent().top;

Try to use parent() like, 尝试使用parent()之类的,

$(selector).parent().offset().top;

Read Get position/offset of element relative to a parent container? 读取元素相对于父容器的位置/偏移量?

Um, you can use like this: 嗯,你可以像这样使用:

$(selector).offsetParent().offset().top;

see documentation on offsetParent 请参阅有关offsetParent的文档

offsetParent doesn't get offset value but it just select the closest positioned parent div. offsetParent没有得到偏移值,但它只是选择最接近定位的父DIV。 You can get the offset/position by using offset and position . 您可以使用偏移位置来获得偏移/ 位置

demo 演示

Don't forget you have to go get offsets all the way to the topmost parent. 不要忘记你必须一直到最顶层的父母。 If you have enough nested content it's probably worth writing a recursive function to get after it. 如果你有足够的嵌套内容,那么编写一个递归函数来获取它可能是值得的。

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

相关问题 如何获取仅相对的offsetParent() - How to get the offsetParent() which is relative only 无法通过jquery在动态生成的div上设置top / left属性 - Unable to set top/left properties on dynamically generated div through jquery 如何获取div或div中的图像的位置(顶部和左侧位置)并在CSS中使用这些值? - How to Get position of a div or an image in the div (top and left position) and use those values in CSS? 如何计算居中div的顶部和左侧位置? - How to calculate top and left positions of a centered div? 如何使用javascript获取像素的left / right / top / bottom属性值? - How to get values of left/right/top/bottom properties in pixel using javascript? 如何从左上角获取可滚动div或svg中的鼠标坐标? - How to get mouse coordinates inside a scrollable div or svg from the top left corner? 无法通过JavaScript设置div.style.left和div.style.top css属性 - Unable to set div.style.left and div.style.top css properties via JavaScript jQuery OnMouseMove滚动div上/下,不是右/左,怎么样? - jQuery OnMouseMove scroll div Top/Bottom, NOT right/left, how? 单击时如何计算具有绝对位置的div的左和上 - How to calculate left and top for div with position absolute when click 如何将 div 滚动到顶部或左侧以查看动态创建的元素? - how to scroll div to top or left to see dynamically created elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM