0

I need to assign a height to an element based on the height of the screen. I have content going past the screen fold, so I can not use

$(window).height(); 

Is there a way to calculate the height of the visible part of the open window?

5
  • What do you mean "height of the visible part"? Which is the invisible part? Commented May 1, 2012 at 19:12
  • 1
    $(window).height(); will show the window size but $(document).height(); will show the whole document height including scrolling part. It should do the job. Commented May 1, 2012 at 19:15
  • @Jon Anything below screen fold is invisible ;) Commented May 1, 2012 at 19:16
  • @Jey, you're right. Not sure what I was thinking. Commented May 1, 2012 at 19:17
  • The invisible part is where we usually keep our cup of coffee Commented May 1, 2012 at 19:30

3 Answers 3

3

Another way to do it is like so:

var height = window.innerHeight || document.documentElement.clientHeight || getElementsByTagName('body')[0].clientHeight; 

This will fallback based on browser support

Sign up to request clarification or add additional context in comments.

Comments

2

This will show you the difference of document or window height.

http://jsfiddle.net/9YFwx/

Comments

2

To get the visible height of the element you would subtract the offetTop of the element from the height of the window.

var elTop = $("#element").offset().top; var winHeight = $(window).height(); var elVisibleHeight = winHeight - elTop; 

If elVisibleHeight < 0 then the element is below the screen fold and not visible. If you needed to keep track of the element's position, you'd need to update this on window scroll.

UPDATE

I've created a fiddle with a working example of the theory. Obviously you can shorten this, I've left it pretty verbose so it's more clear as to what is happening.

Example Fiddle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.