Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

window resize issue

Posted on 2016-07-15
12
Medium Priority
?
77 Views
Last Modified: 2016-07-16
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
Comment
Question by:Neil_Bradley
  • 8
  • 4
12 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41711868
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41711882
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41712605
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
Industry Leaders: 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!

 
LVL 22

Expert Comment

by:Kim Walker
ID: 41712774
You're not changing WindowHeight in your else clause. So it would be using the WindowHeight from the previous > 1024 resize.
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41713554
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41713572
I have created a fiddle here
So I simply need to pass a new window height into my else clause?
Cheers,
N
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41713783
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41713787
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41713788
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 41713794
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
 
LVL 22

Accepted Solution

by:
Kim Walker earned 2000 total points
ID: 41713800
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
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 41713867
Thank you so much for sticking with this one. Great work appreciated.
Cheers.
N
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question