-
-
Save ryarwood/1332495 to your computer and use it in GitHub Desktop.
Change DOM elements with jquery on screen re-size
This file contains 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
(function( $ ){ | |
$.fn.resize = function(max_size) | |
{ | |
return this.each(function() | |
{ | |
//Wait until the image is loaded | |
$(this).load(function() | |
{ | |
//Create a container div | |
var div_container = $("<div></div>"); | |
div_container.css({'width': max_size}); | |
//Get the new size of the image | |
if ($(this).height() > $(this).width()) | |
{ | |
var h = max_size; | |
var w = Math.floor($(this).width() / $(this).height() * max_size); | |
} | |
else | |
{ | |
var w = max_size; | |
var h = Math.floor($(this).height() / $(this).width() * max_size); | |
} | |
//Centralize the image | |
//var top = Math.floor((120-h)/2); | |
//var left = Math.floor((120-w)/2); | |
//Resize the image and put it in the container div | |
$(this).css({ 'height': h, 'width': w}); | |
$(this).parent().append( | |
div_container.append($(this).detach()) | |
); | |
}); | |
}); | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment