简体   繁体   English

如何获得元素的正确偏移? - jQuery

[英]how to get right offset of an element? - jQuery

This is probably a really simple question, but how do I go about getting the right offset of an element in jQuery? 这可能是一个非常简单的问题,但我如何在jQuery中获得元素的正确偏移?

I can do: 我可以:

$("#whatever").offset().left;

and it is valid. 它是有效的。

But it seems that: 但似乎:

$("#whatever").offset().right 

is undefined. 未定义。

So how does one accomplish this in jQuery? 那么如何在jQuery中实现这一目标呢?

Thanks!! 谢谢!!

Alex, Gary: 亚历克斯,加里:

As requested, here is my comment posted as an answer: 根据要求,这是我的评论张贴作为答案:

var rt = ($(window).width() - ($whatever.offset().left + $whatever.outerWidth()));

Thanks for letting me know. 谢谢你让我知道。

In pseudo code that can be expressed as: 在伪代码中,可以表示为:

The right offset is: 正确的抵消是:

The window's width MINUS 窗口的宽度为 MINUS
( The element's left offset PLUS the element's outer width ) (元素的左偏移加上元素的外部宽度

var $whatever        = $('#whatever');
var ending_right     = ($(window).width() - ($whatever.offset().left + $whatever.outerWidth()));

Reference: .outerWidth() 参考: .outerWidth()

Maybe I'm misunderstanding your question, but the offset is supposed to give you two variables: a horizontal and a vertical. 也许我误解了你的问题,但偏移应该给你两个变量:水平和垂直。 This defines the position of the element. 这定义了元素的位置。 So what you're looking for is: 所以你要找的是:

$("#whatever").offset().left

and

$("#whatever").offset().top

If you need to know where the right boundary of your element is, then you should use: 如果您需要知道元素的右边界在哪里,那么您应该使用:

$("#whatever").offset().left + $("#whatever").outerWidth()

Just an addition to what Greg said: 只是格雷格所说的一个补充:

$("#whatever").offset().left + $("#whatever").outerWidth() $(“#whatever”)。offset()。left + $(“#whatever”)。outerWidth()

This code will get the right position relative to the left side. 此代码将相对于左侧获得正确的位置。 If the intention was to get the right side position relative to the right (like when using the CSS right property) then an addition to the code is necessary as follows: 如果目的是获得相对于右侧的右侧位置(就像使用CSS right属性时那样),则需要添加代码,如下所示:

$("#parent_container").innerWidth() - ($("#whatever").offset().left + $("#whatever").outerWidth()) $(“#parent_container”)。innerWidth() - ($(“#whatever”)。offset()。left + $(“#whatever”)。outerWidth())

This code is useful in animations where you have to set the right side as a fixed anchor when you can't initially set the right property in CSS. 此代码在动画中非常有用,当您无法在CSS中初始设置right属性时,必须将右侧设置为固定锚点。

Brendon Crawford had the best answer here (in comment), so I'll move it to an answer until he does (and maybe expand a little). Brendon Crawford在这里得到了最好的答案(在评论中),所以我会把它移到一个答案,直到他做(并可能扩大一点)。

var offset = $('#whatever').offset();

offset.right = $(window).width() - (offset.left + $('#whatever').outerWidth(true));
offset.bottom = $(window).height() - (offset.top + $('#whatever').outerHeight(true));

Actually these only work when the window isn't scrolled at all from the top left position. 实际上这些仅在窗口没有从左上角位置滚动时才起作用。
You have to subtract the window scroll values to get an offset that's useful for repositioning elements so they stay on the page: 您必须减去窗口滚动值以获得对重新定位元素有用的偏移量,以便它们保留在页面上:

var offset = $('#whatever').offset();

offset.right = ($(window).width() + $(window).scrollLeft()) - (offset.left + $('#whatever').outerWidth(true));
offset.bottom = ($(window).height() + $(window).scrollTop()) - (offset.top + $('#whatever').outerHeight(true));

有一个本机DOM API可以实现开箱即用 - getBoundingClientRect

document.querySelector("#whatever").getBoundingClientRect().right

Getting the anchor point of a div/table (left) = $("#whatever").offset().left; 获取div/table (left) = $("#whatever").offset().left;的锚点div/table (left) = $("#whatever").offset().left; - ok! - 好!

Getting the anchor point of a div/table (right) you can use the code below. 获取div/table (right)的锚点div/table (right)您可以使用下面的代码。

 $("#whatever").width();

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

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