Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

window resize issue

I've enclosed my "fulHeight" function in a resize function so I can apply on the fly if the screen size is above 1024 px.
Seems to work if I resize from narrow screen to wide but not wide screen to narrow. Can anyone spot the error in my jquery?
added a resize function to turn the full height function on and off
var WindowHeight = jQuery(window).height();
		var HeaderHeight = 0;
		
	
if ($(window).width() > 1024) {
		jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
}
		
        var windowsize = $(window).width();
		jQuery(window).resize(function() {
			windowsize = $(window).width();
			if (windowsize > 1024) {
		  var WindowHeight = jQuery(window).height();
		  jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
			}
		});

Open in new window

Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Your code is only executed if the window width exceeds 1024 after the resize.
if (windowsize > 1024) { ... }

Open in new window

But there is nothing in your resize code that would undo this when the screen width is less than 1024 after the resize.

The condition would be re-evaluated on each resize and the height will be adjusted when the window size is more than 1024, but nothing will be done when the window size is less than 1024. The previous adjustment will prevail.
Avatar of Neil_Bradley

ASKER

Hi Kim,
so Ive updated to this but still no joy:
		var WindowHeight = jQuery(window).height();
		var HeaderHeight = 0;
		
	
if (windowsize > 1024) {
		jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
}
		
        var windowsize = $(window).width();
		
		jQuery(window).resize(function() {
			windowsize = $(window).width();
			if (windowsize > 1024) {
		  var WindowHeight = jQuery(window).height();
		  jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
			}else{
				
				jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
			}
		});

Open in new window

Here is the page in question
If you inspect the .fullheight element you will see the function in action. Start with a narrow screen and enlarge past 1024px and you will see the function kick in. Try from wide to narrow and function should stop working but it carries on applying full height.
Cheers,
N
You're not changing WindowHeight in your else clause. So it would be using the WindowHeight from the previous > 1024 resize.
Hi Kim,
makes sense but I may need a nudge to help me to a solution.
So I would need to add  var WindowHeight = jQuery(window).height(); again to my else?
I have tried a few variations to try and achieve this but with no luck.
I have created a fiddle here
So I simply need to pass a new window height into my else clause?
Cheers,
N
There's nothing in any of your posts that suggests what you intend to happen when the window height is less than 1024 except in your first post where you state that you
added a resize function to turn the full height function on and off
But there are no details to suggest what on and off are. Apparently you're happy with the on state.

But if you add var WindowHeight = jQuery(window).height(); to your else clause there would not be any difference between the on (if) and off (else) states. The most obvious option for the off state would be to undo the on state -- remove the inline css height property added in the on (if) clause by setting the height property to an empty string.
var WindowHeight = jQuery(window).height();
var HeaderHeight = 0;


if (windowsize > 1024) {
	jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
}

var windowsize = $(window).width();

jQuery(window).resize(function() {
	windowsize = $(window).width();
	if (windowsize > 1024) {
		var WindowHeight = jQuery(window).height();
		jQuery(".fullheight").css("height", WindowHeight - HeaderHeight);
	}else{
		jQuery(".fullheight").css("height", "");
	}
});

Open in new window

Hi Kim,
to clarify (and apologies if my posts were not clear).
I want the full height function to effect the div  described in the fiddle here
only if the window width is above 1024px. By effect I mean push it to the full height of the page.
If the page is under 1024px or is reduced to under that with after the page is open I want to stop the fullHeight function from pushing the div to full height.

Hope this helps.
N
Kim,
I have just updated the fiddle with your code and the resizing works perfectly!
Only issue now is that if the page opens and the window height is more than 1024 initially then the function does not work until resize occurs..
N
I've updated the fiddle to fix the above mentioned issue.
Unless you have any further comments I will close this question..
Cheers,
N
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
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
Thank you so much for sticking with this one. Great work appreciated.
Cheers.
N