On 06/08/2007, at 11:36 PM, LSUDVM wrote:
function resize(which, max) {
  var elem = document.getElementById(which);
  if (elem == undefined || elem == null) return false;
  if (max == undefined) max = 100;
  if (elem.width >  elem.height) {
    if (elem.width > max) elem.width = max;
  } else {
    if (elem.height > max) elem.height = max;
  }
}

Written as a plugin (very quickly and untested so beware), it might look like this:

$.fn.resize = function(max){
        max = max || 100;
        return this.each(function(i){
                var w = this.width,
                        h = this.height;
                if (w > h){
                        if (w > max) {
                                this.width = max;
                        }
                } else if (h > max){
                        this.height = max;
                }
        });
};

//then you can use it like this (within a $(document).ready() block probably):
$('#myElemId').resize(200);

//or you don't have to use an id at all, and could apply it to multiple elements with a class:
$('.myElemsClass').resize(200);

...or select the elements using any form of jQuery selector. It is also chainable.

Note, not only have I not tested this but I didn't really think deeply into the logic of of your conditionals either, so its just a rewrite. Just a warning ;)

Joel Birch.



Reply via email to