Skip to content

Instantly share code, notes, and snippets.

@madmax
Created May 8, 2010 12:57
Show Gist options
  • Save madmax/394550 to your computer and use it in GitHub Desktop.
Save madmax/394550 to your computer and use it in GitHub Desktop.
var ImageResize = {
margin: 8,
parse: function(css_id, margin) {
if(margin) { this.margin = margin; }
$$('#' + css_id + ' img').each(function(img) {
this.resize(img, $(css_id).getWidth() - this.margin);
}, this);
},
resize: function(img, max_width) {
// if image is bigger that max_width we add them width = max_width and don't check image again
if (img.width > max_width) {
img.style.width = max_width + 'px';
return true;
}
// if image isn't complete loaded just bind to check size again after 5 ms
if (!img.complete) {
setTimeout(function() {
ImageResize.resize(img, max_width);
}, 5);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment