Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created October 31, 2012 02:28
Show Gist options
  • Select an option

  • Save akkunchoi/3984452 to your computer and use it in GitHub Desktop.

Select an option

Save akkunchoi/3984452 to your computer and use it in GitHub Desktop.
jQuery image max-width, max-height
// usage:
// $('img').resizer({maxWidth: 100});
$.fn.resizer = function(options){
this.each(function(k, v){
var d;
var self = $(v);
self.on('load', function(){
if (options.maxWidth){
if (self.width() > options.maxWidth){
d = options.maxWidth / self.width();
self.height(self.height() * d);
self.width(options.maxWidth);
}
}
if (options.maxHeight){
if (self.height() > options.maxHeight){
d = options.maxHeight / self.height();
self.width(self.width() * d);
self.height(options.maxHeight);
}
}
});
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment