Link to home
Start Free TrialLog in
Avatar of cycledude
cycledudeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

jquery $(window).height() not working as expected

HI

I am trying to create some jquery that will resize an image  to almost full screen, regardless of the window size... but I am having a little trouble...

in my css I have the image set to 100% width, and when the screen is resized, the image is resized correctly and with proportions maintained.. that is until you take it to a screen size where it is taller than the image, which is when I see a gap at the bottom, so in this circumstance I need to stop the heigh changing and make the width change so that less of the image is being shown (I hope this makes sense)


this cannot be done with css alone, so i turned to jquery to assist...

I figure that if I can capture the height of the browser window, and then check if the image height is less than or equal to that, then make the width of the image widen.


eg:

var $minHeight = 500;


 $( window ).resize(function() {
	
		$w = $(window).outerWidth(true);
		$h = $(window).outerHeight(true);
		
		if ($h <= $minHeight) {

                   //some code to adjust the width of the image by x pixels
                }
			
	});

Open in new window



but it turns out that the $(window).height(); function is just returning the height of my image (there is a header and footer fixed to the top and bottom of the page)

so all I am getting is the image height not the window height.... any ideas how I can get around this?

or has anyone got any code that does the function I require?
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

$(window).height(); returns the height of the window. $(document).height(); returns the height of your document, and could potentially return the height of your image if your document is as tall as your image.

Hence, are you sure you are reading your code output correctly?

Please post a link to your page so people can trouble shoot.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cycledude

ASKER

Hi

OK, I have put a sample page up, the structure is pretty much what I am working on

http://your-website.co.uk/ee/jquery-window-height-problem/
the windows height is being dictated by the height of the wrap, i think...
Thank you.  I will come back to this later today.
you can always use:

$h = window.innerHeight

though this may return slightly different values in different browsers, so you may need some additional code to normalize it. It seems there is a bug in the jquery version
the way the above plugin does it is by injecting an element, and measuring that element to get the window dimensions:

#supersized { position:fixed; left:0; top:0; overflow:hidden; z-index:-999; height:100%; width:100%; }

they don't use $(window).height(), so that tells you something.
In chrome for mac, the image looks good as I adjust the browser width.
@padas, He's not manipulating the image at all in this example. the image is only shrinking via CSS. If you resize your browser to a small enough height the window height property is not correct, as  cycledude said.
i did not get a resolution to this...
I've requested that this question be closed as follows:

Accepted answer: 0 points for cycledude's comment #a39643878

for the following reason:

problem not solved
This is a working solution:

https://www.experts-exchange.com/questions/28271432/jquery-window-height-not-working-as-expected.html?anchorAnswerId=39586121#a39586121

And this post explains how the plugin that was given as an example does it.

https://www.experts-exchange.com/questions/28271432/jquery-window-height-not-working-as-expected.html?anchorAnswerId=39586124#a39586124

So the problem was solved, the author just didn't like the solution, or wasn't able to implement it, that's a different thing.
you cant post a` link after i close the question then claim that i had ignored it ?
its not a link. its a reference to a prior post.
this is a solution to getting a full sized background image, however for my purposes it was not usable due to client requirements.

thanks