2

Currently, I'm setting width and height of some elements with jQuery in window resize event, based on window.innerWidth and window.innerHeight.

Something like this:

$(".gr1").css("height", (window.innerHeight - 150) + "px");

And these are some other examples:

$(".gr2").css("width", ((window.innerWidth / 2) - 12).toString() + "px");
$(".box1").css("height", ((window.innerHeight - 150) / 3 - 12).toString() + "px");
$(".box2").css("height", ((window.innerHeight - 150) / 2 - 12).toString() + "px");

And this is rendering a little slow in Chrome 21 (Windows) and renders too slow in iPad and Nexus 7 tablet. (Since there are many elements with gr1, gr2, box1 and box2 classes)

Can I do this in Pure CSS to give better performance? How?

Mahdi Ghiasi
  • 14,873
  • 19
  • 71
  • 119
  • Please show us your DOM. Are the elements unique, which styles do they already have? – Bergi Sep 03 '12 at 12:50
  • @Bergi My DOM is messy, also it has many other things. Anyway, I'll post a link to it in a few minutes. But, Why you need DOM? – Mahdi Ghiasi Sep 03 '12 at 12:51
  • Why would a mechanic need a car to see what's wrong with it? "My car has a problem, here's a description of it, now fix it!" – Kyle Sep 03 '12 at 12:54
  • @MahdiGhiasi: There is no layout with nothing to display. What are these elements you want to resize, where are they in your page? Only their contents is irrelevant – Bergi Sep 03 '12 at 12:54
  • Maybe if you would optimized your selectors the performance would be satysfactory. This may be possible - but depends on your DOM and/or usecase. – WTK Sep 03 '12 at 12:54
  • There are quite a few things you could do to improve the performance of what you already have. By the looks of it each time you are calling the resize event you are re-searching the DOM for all the elements you want to change, again and again. First off, save this list to a variable. i.e. `$gr2 = $('.gr2');` and then inside your resize function access using `$gr2.css('width', ...);`. Better still don't use jQuery for a simple operation as setting width or height. Use something like `var i = $gr2.length, h = (window.innerWidth / 2 - 12) + "px"; while(i--){$gr2[i].style.height = h;}` – Pebbl Sep 03 '12 at 12:58

4 Answers4

6

Without seeing your HTML or CSS I can't give you a definitive answer, but the best solution if you need the elements of your page to react to the browser window being resized is to use % widths, combined with min-width and max-width to set concrete size limits.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
4

you could try to cache your jQuery selectors, improve performance:

var gr1 = $('.gr1');
var gr2 = $('.gr2');
var box1 = $('.box1');
var box2 = $('.box2');
$(gr1).css("height", (window.innerHeight - 150) + "px");
$(gr2).css("width", ((window.innerWidth / 2) - 12).toString() + "px");
$(box1).css("height", ((window.innerHeight - 150) / 3 - 12).toString() + "px");
$(box2).css("height", ((window.innerHeight - 150) / 2 - 12).toString() + "px");
Mark
  • 6,762
  • 1
  • 33
  • 50
3

You could try using calc(), but the support is not very good (limited to IE9+, FF4+, Safari 6, Chrome 19+ & Firefox for Android).

Test: http://dabblet.com/gist/3609183

Ana
  • 35,599
  • 6
  • 80
  • 131
2

Media Queries Would possibly be the way to go, though it depends on your definition of "Pure" CSS, as media queries are new to CSS3, and don't work fully in IE 6/7/8.

If you don't know, they work by only applying certain styling based upon the screen/devices current resolution. You can see an example, resize the window and see how the content changes?

This is referred to as responsive web design

Alexander Wigmore
  • 3,157
  • 4
  • 38
  • 60