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?
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.
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).
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();
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())
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