Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get visible height of an element instead of its actual height with jquery

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!

like image 716
Tom S. Avatar asked Jan 02 '13 17:01

Tom S.


People also ask

Which jQuery method is used to show selected elements by adjusting height?

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.

How do I set dynamic height in jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

How do I make my window visible height?

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.

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).


2 Answers

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.

like image 103
Blake Plumb Avatar answered Oct 10 '22 05:10

Blake Plumb


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.

like image 40
Bryan Allo Avatar answered Oct 10 '22 06:10

Bryan Allo