Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

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

0
Neil_Bradley
Asked:
Neil_Bradley
  • 8
  • 4
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
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.
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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

0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Kim WalkerWeb Programmer/TechnicianCommented:
You're not changing WindowHeight in your else clause. So it would be using the WindowHeight from the previous > 1024 resize.
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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.
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
I have created a fiddle here
So I simply need to pass a new window height into my else clause?
Cheers,
N
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
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

0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
I've updated the fiddle to fix the above mentioned issue.
Unless you have any further comments I will close this question..
Cheers,
N
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
function does not work until resize occurs..
You actually have two separate functions running in your code. Lines 5-7 are executed when the page loads to determine if the initial window is larger that 1024 pixels wide. The issue is probably with where on the page this code has been inserted. If you have this code in the head section of the page, the target div with the class name .fullheight has not be added to the DOM yet, so it cannot be altered yet. If you have the code at the bottom of the body element, it should work as expected.

If you prefer to keep your script code in the head, you will need to defer the execution of that script until the page has finished loading. In the script below I've eliminated some redundancies and added the page ready deferment.
var WindowHeight = jQuery(window).height();
var HeaderHeight = 0;
var windowsize = $(window).width();

jQuery(window).resize(large_screen);	// execute on window resize
jQuery(large_screen);			// execute on page load

function large_screen() {
	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

0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thank you so much for sticking with this one. Great work appreciated.
Cheers.
N
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now