Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get the height from top

I need to get the height from top of the page to current scrollbar position and place it to my link:

<a class="poplight" rel="popup_name" href="#?w=here comes the value"></a>

How can I do this?

like image 266
Daniel Koczuła Avatar asked Jun 18 '12 15:06

Daniel Koczuła


People also ask

How can get current Div height in jQuery?

jQuery height() Method The height() method sets or returns the height of the selected elements. When this method is used to return height, it returns the height of the FIRST matched element. When this method is used to set height, it sets the height of ALL matched elements.

What is offsetHeight in jQuery?

offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).

How does jQuery calculate window height?

height() method is recommended when an element's height needs to be used in a mathematical calculation. This method is also able to find the height of the window and document. $( document ). height();


1 Answers

Use the offset()

$('.poplight').offset().top

If you need to scroll to that position:

$('html, body').animate({
    scrollTop: $('.poplight').offset().top
}, 400);

If you need the distance from the top of the window to the current position based on scroll:

$(window).scrollTop()

Want to add that to the url:

$(".poplight").attr("href", "#" + $(window).scrollTop())
like image 167
Rene Pot Avatar answered Oct 11 '22 23:10

Rene Pot