Get bottom and right position of an element

Jquery

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

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;

Solution 2 - Jquery

var link = $(element);
var offset = link.offset();

var top = offset.top; var left = offset.left;

var bottom = top + link.outerHeight(); var right = left + link.outerWidth();

Solution 3 - Jquery

// Returns bottom offset value + or - from viewport top
function offsetBottom(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().bottom }

// Returns right offset value
function offsetRight(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().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. So say your logo was 350px and it had a left margin of 50px, variable 'right' will hold a value of 400 because that's the actual distance in pixels it took to get to the edge of your element, no matter if you have more padding or margin to the right of it.

If your box-sizing CSS property is set to border-box it will continue to work just as if it were set as the default content-box.

Solution 4 - Jquery

You can use the .position() for this

var link = $(element);
var position = link.position(); //cache the position
var right = $(window).width() - position.left - link.width();
var bottom = $(window).height() - position.top - link.height();

Solution 5 - Jquery

Here is a jquery function that returns an object of any class or id on the page

var elementPosition = function(idClass) {
			var element = $(idClass);
			var offset = element.offset();

			return {
				'top': offset.top,
				'right': offset.left + element.outerWidth(),
				'bottom': offset.top + element.outerHeight(),
				'left': offset.left,
			};
		};


		console.log(elementPosition('#my-class-or-id'));

Solution 6 - Jquery

Vanilla Javascript Answer

var c = document.getElementById("myElement").getBoundingClientRect();
var bot = c.bottom;
var rgt = c.right;

To be clear the element can be anything so long as you have allocated an id to it <img> <div> <p> etc.

for example

<img
    id='myElement'
    src='/img/logout.png'
    className='logoutImg img-button'
    alt='Logout'
/>

Solution 7 - Jquery

I think

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div>Testing</div>
<div id="result" style="margin:1em 4em; background:rgb(200,200,255); height:500px"></div>
<div style="background:rgb(200,255,200); height:3000px; width:5000px;"></div>

<script>
(function(){
	var link=$("#result");
	
	var top = link.offset().top; // position from $(document).offset().top
	var bottom = top + link.height(); // position from $(document).offset().top
	
	var left = link.offset().left; // position from $(document).offset().left
	var right = left + link.width(); // position from $(document).offset().left

	var bottomFromBottom = $(document).height() - bottom;
    // distance from document's bottom
	var rightFromRight = $(document).width() - right;
    // distance from document's right

	var str="";
	str+="top: "+top+"<br>";
	str+="bottom: "+bottom+"<br>";
	str+="left: "+left+"<br>";
	str+="right: "+right+"<br>";
	str+="bottomFromBottom: "+bottomFromBottom+"<br>";
	str+="rightFromRight: "+rightFromRight+"<br>";
	link.html(str);
})();
</script>

The result are

top: 44
bottom: 544
left: 72
right: 1277
bottomFromBottom: 3068
rightFromRight: 3731

in chrome browser of mine.

When the document is scrollable, $(window).height() returns height of browser viewport, not the width of document of which some parts are hiden in scroll. See http://api.jquery.com/height/ .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCameronView Question on Stackoverflow
Solution 1 - JqueryMordhakView Answer on Stackoverflow
Solution 2 - JqueryDamianView Answer on Stackoverflow
Solution 3 - JqueryKerry JohnsonView Answer on Stackoverflow
Solution 4 - JqueryStarxView Answer on Stackoverflow
Solution 5 - JquerypurencoolView Answer on Stackoverflow
Solution 6 - JqueryMichael NellesView Answer on Stackoverflow
Solution 7 - JquerykipidView Answer on Stackoverflow