-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.resize.js
51 lines (47 loc) · 1.32 KB
/
jquery.resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Resize, A resize plugin for jQuery
* Intructions: $(seletor).resize(max_size)
* By: Carlos Alessandro Sena de Freitas - [email protected]
* Version: 1.0
* Updated: September 1st, 2011
*
* Licensed under the GNU - General Public License
* You may obtain a copy of the License at
*
* http://www.gnu.org/copyleft/gpl.html
*
*/
(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({ 'height': max_size, 'width': max_size, 'position': 'absolute'});
//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, 'top': top, 'left': left, 'position': 'absolute'});
$(this).parent().append(
div_container.append($(this).detach())
);
});
});
};
})( jQuery );