Created
December 20, 2011 22:26
-
-
Save andershaig/1503577 to your computer and use it in GitHub Desktop.
Image Thumbnails using JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example: all images with a class of .img get sized to 150px. | |
makeThumbs({ | |
selector: '.img', | |
size: 150 | |
}); | |
function makeThumbs() { | |
var selector = args.selector; | |
var thumb_size = args.size; | |
// TODO - need to wrap with another item that has overflow hidden to hide the extra parts. | |
$(selector + ':visible').not('.processed').each( function () { | |
var img_width = $(this).width(); | |
var img_height = $(this).height(); | |
if (img_width > img_height) { | |
$(this).css('height', thumb_size + 'px'); | |
var new_width = ($(this).width() - thumb_size) * -0.5; | |
$(this).css('left', new_width); | |
} else if (img_width < img_height) { | |
$(this).css('width', thumb_size + 'px'); | |
var new_height = ($(this).height() - thumb_size) * -0.5; | |
$(this).css('top', new_height); | |
} else if (img_width == img_height) { | |
$(this).css({'width':thumb_size + 'px', 'height':thumb_size + 'px'}); | |
}; | |
// Mark as processed | |
$(this).addClass('processed'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment