This post is related to this one. Please consider reading it as well. :-)
In the post I linked to, I figured the solution to my problem would be to change the target of a link if the visible height of a div is greater than that of another div. In my layout, all the divs I'm referring to have a height of 1100px. But that's not what I want to get. I'd like the script to get height of the div that is currently visible to the visitor, not its real height. Is there a way to do it using jQuery?
Thanks in advance!
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.
Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.
To obtain the height of the window minus its horizontal scroll bar and any borders, use the root <html> element's clientHeight property instead. Both innerHeight and innerWidth are available on any window or any object that behaves like a window, such as a tab or frame.
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).
What you can do is take the elements position to the top of its parent container and then minus it from the parents container height. That will give you the visible height of the element.
$('#container').height() - $('#overflow').position().top
Here is a fiddle showing this.
Wrap the contents of the viewport DIV with another DIV. Target that DIV to read the height of all the contents as such:
JQuery: $('div#contents').height();
Layout:
<div id="viewport">
<div id="contents">...all your div contents...</div>
</div>
Hope this helps. Good Luck.
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