Is re-sizing DIVs on window-resize with Javascript too heavy for most browsers/computers? I’m doing this, because just setting widths in percentages only works in relation to the parent element. I can’t seem to make it a percentage of the body, without regard to other parent elements between (on the DOM tree) the elements I would like to resize and the body.
function resize_columns() {
var d = getWindowSize();
var body_width = d.x;
var fch_width = body_width * .15;
var pnh_width = body_width * .25;
$('.for_customer_div').css('width', fch_width + 'px');
$('.for_customer_header').css('width', fch_width + 'px');
$('.project_name_header, .project_name_div').css('width', pnh_width + 'px');
$('.line_item_div').css('width', pnh_width + 'px');
}
$(document).ready(function() {
resize_columns();
});
$(document).ready(function() {
window.onresize = function(event) {
resize_columns();
}
});
function getWindowSize() {
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight || e.clientHeight || g.clientHeight;
var dimensions = {
x: x,
y: y
}
Try adding something like this in your code:
And then call:
This will limit the number number of times
resize_columnswill be called to a maximum of once every 20 milliseconds.Explanation:
The throttle function accepts two parameters: the function you want to execute, and a timeout duration. It then returns a new function with a reference to the
throttlingvariable, which acts as a flag. So, as far asonresizeis concerned,throttlejust looks like the inner function it returns. Every time that function is executed, it says “isthrottlingset tofalse?”. And if it is, it goes ahead and executes your function. Then it setsthrottlingtotrueand creates asetTimeoutthat will wait x number of milliseconds before it setsthrottlingback tofalse. If it gets executed andthrottlingis set totrue, it doesn’t do anything.So the basic effect is that, at most, your function can only be executed once in a given time window. So you’re just rate-limiting.
I know that might not be a great explanation, but I think a full-blown overview of closures would be biting off a little more than I’m willing to chew this morning.