How to get the correct element offset? - jQuery

This is probably a very simple question, but how do I get the correct element offset in jQuery?

I can do:

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

and it really is.

But it seems that:

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

- undefined.

So how to do this in jQuery?

Thank!!

+67
jquery offset
Jun 15 '10 at 6:56
source share
8 answers

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.

+124
Jun 21 2018-12-12T00:
source share
 var $whatever = $('#whatever'); var ending_right = ($(window).width() - ($whatever.offset().left + $whatever.outerWidth())); 

Link:. outerWidth ()

+27
Jun 15 '10 at 6:58
source share

Perhaps I do not understand your question, but the offset should give you two variables: horizontal and vertical. This determines the position of the element. So you are looking for:

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

and

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

If you need to know where the right border of your element is, you should use:

 $("#whatever").offset().left + $("#whatever").outerWidth() 
+13
Jun 15 '10 at 6:59
source share

Just an addition to what Greg said:

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

This code will get the correct position relative to the left side. If the goal was to get the right position with respect to the right (for example, when using the CSS right property), then adding to the code is necessary as follows:

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

This code is useful in animations where you should set the right side as a fixed anchor if you cannot initially set the right property in CSS.

+7
Apr 13 '11 at 3:01
source share

Brendan Crawford had the best answer here (in the commentary), so I will translate it before the answer until it does (and possibly expands a bit).

 var offset = $('#whatever').offset(); offset.right = $(window).width() - (offset.left + $('#whatever').outerWidth(true)); offset.bottom = $(window).height() - (offset.top + $('#whatever').outerHeight(true)); 
+3
May 14 '12 at 2:54 a.m.
source share

In fact, they only work when the window does not scroll at all from the upper left position. You must subtract the scroll values โ€‹โ€‹of the window to get the offset, which is useful for repositioning the elements so that 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)); 
+3
Jul 18 2018-12-18T00:
source share

There is a native DOM API that reaches this out of the box - getBoundingClientRect :

 document.querySelector("#whatever").getBoundingClientRect().right 
+2
Apr 13 '16 at 11:52
source share

Getting the anchor point in div/table (left) = $("#whatever").offset().left; - OK

Having received the anchor point div/table (right) , you can use the code below.

  $("#whatever").width(); 
+1
Apr 18 '13 at 9:37
source share



All Articles