Solved

window resize issue

Posted on 2016-07-15
12
44 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 21

Expert Comment

by:Kim Walker
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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
Comment Utility
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
Comment Utility
I have created a fiddle here
So I simply need to pass a new window height into my else clause?
Cheers,
N
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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
Comment Utility
Thank you so much for sticking with this one. Great work appreciated.
Cheers.
N
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to dynamically set the form action using jQuery.
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)

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now