forked from miyagawa/jquery-fitimage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.fitimage.js
71 lines (62 loc) · 2.09 KB
/
jquery.fitimage.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function($) {
$.fn.fitimage = function(options) {
if (!options) options = {};
var defaults = {
placeholder: "data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAEBMgA7"
};
this.each(function(){
var $el = $(this);
// Swap the img@src with the placeholder
var src = $el.attr('src');
$el.attr('src', options.placeholder || defaults.placeholder);
var width = options.width || $el.width();
var height = options.height || $el.height();
var img = new Image();
img.onerror = function() {
alert("error");
};
img.onload = function() {
// Check if the loaded image is wide or tall
var is_tall = this.height > this.width * height / width;
if (is_tall) {
size = height * this.width / this.height;
margin = (width - size) / 2;
} else {
size = width * this.height / this.width;
margin = (height - size) / 2;
}
if (margin < 0) margin = 0;
// Adjust margin-* with the current value
var adjuster = function(name, margin) {
var current = $el.css(name);
if (current && current.match(/^(\d+)px$/)) {
return parseInt(RegExp.$1) + margin;
}
return margin;
};
// Update margin and width/height CSS properties
if (is_tall) {
$el.css({
height: height,
width: size,
"margin-left": adjuster("margin-left", margin),
"margin-right": adjuster("margin-right", margin)
});
} else {
$el.css({
width: width,
height: size,
"margin-top": adjuster("margin-top", margin),
"margin-bottom": adjuster("margin-bottom", margin)
});
}
// Finally shoves the loaded img@src
$el.attr("src", this.src);
// patch for IE6/7 problem(multiple call with animation GIF)
img.onload = null;
};
// load the target image and fire the callbacks
img.src = src;
});
};
})(jQuery);