Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bottom and right position of an element

Tags:

jquery

I'm trying to get the position of an element within the window like so:

var link = $(element);  var offset = link.offset(); var top = offset.top; var left = offset.left; var bottom = $(window).height() - link.height(); bottom = offset.top - bottom; var right = $(window).width() - link.width(); right = offset.left - right; 

However the bottom and right have - in front of them... Why is this? as the numbers are correct just they should NOT be minus.

like image 424
Cameron Avatar asked Mar 26 '12 12:03

Cameron


People also ask

How do you find the bottom of an element?

The offset method will return the coordinates of the element relative to the page. We can then get the top coordinate from this method. To get the bottom position of the element, we will have to add the height of the element to this number, using the height() method.

How do I get the position of an element in HTML?

offset() will get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.

How do I get offsetBottom?

The jQuery offset() function is a built-in function in jQuery, by which we can return the offset of the bottom coordinate. The jQuery offset() function only specifies the top and left properties position, but with the help of top property and outerHeight() function, we can get the bottom position for elements.

How do you get the position of the bottom of an element in JavaScript?

right } var bottom = offsetBottom('#logo'); var right = offsetRight('#logo'); This will find the distance from the top and left of your viewport to your element's exact edge and nothing beyond that.


1 Answers

Instead of

var bottom = $(window).height() - link.height(); bottom = offset.top - bottom; 

Why aren't you doing

var bottom = $(window).height() - top - link.height(); 

Edit: Your mistake is that you're doing

bottom = offset.top - bottom; 

instead of

bottom = bottom - offset.top; // or bottom -= offset.top; 
like image 191
Mordhak Avatar answered Sep 20 '22 20:09

Mordhak