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.
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With