1

I am trying to accomplish a positioning scheme that is hard with CSS alone. I could do it easily with JavaScript, using something like:

function onWindowResize(new_width,new_height){
    $('#element')
       .css({  left: some_function(new_width),
               top: another_function(new_height) });
};

Is it OK to use that approach? What would be possible drawbacks?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
  • Is it impossible to do with media-queries? – João Paulo Macedo Sep 21 '12 at 23:06
  • 3
    Make sure to throttle the resize event so that you aren't performing an expensive operation dozens/hundreds of times during the resize: http://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-or-resize-event-and-only-than-perform-action – Tim M. Sep 21 '12 at 23:10

2 Answers2

4

Sure, it's perfectly fine to do it with javascript if you want. Some resize operations require more complicated logic than can be expressed in pure CSS.

In many cases, it can be done with pure CSS too. If you show us what you were really trying to accomplish, you might get some CSS-only suggestions.

Potential drawbacks:

  1. Your page requires javascript in order to function properly (not much of an issue these days in my opinion).
  2. The layout may not track quite as smoothly during resizing of the window compared to a CSS only solution.
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

Two drawbacks:

  • Won't work if user has JS disabled
  • Ties your layout to your JS, preferable to keep it in CSS
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217