-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fitfill.js
106 lines (100 loc) · 3.53 KB
/
jquery.fitfill.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
jQuery fitFill
Ben Donaldson Copyright 2013
(MIT / I don't care what you do with it except send an email to [email protected] or something and be like "hey, thanks") License
THE ELEMENT YOU USE THIS ON MUST:
* BE THE PARENT CONTAINER OF THE <img> YOU WANT TO SCALE
* HAVE A WIDTH AND A HEIGHT EXPLICITLY SET IN CSS, LIKE "width: 50px" or "width: 100%"
methods:
If there's only one image in the container:
fit() scales the image proportionally to fit in the container
fill() scales the image proportionally to fill the container
If there's more than one image
use fitAll() instead of fit()
use fillAll() instead of fill()
options:
centerW: centers the image horizontally, default true
centerH: centers the image vertically, default true
parentEl: specify a different element to scale the image to, instead of the current one. Like if the image's parent has no width and height CSS, but the parent's parent does (seriously, that comes up)
success: function to call when the image successfully loads (function is passed the image element as an argument)
error: function to call when the image doesn't load (function is passed the image element as an argument)
Ways to use it:
$('#example_image').fit();
$('#example_images img').fitAll();
$('#example_image').fit({
centerH: false
});
$('#example_image').fit({
error: function(imgEL) {
alert("shut down everything");
}
});
*/
!(function ($) {
var scale = function (imgEl, imgHasDims, options) {
options = $.extend({
centerW: true,
centerH: true,
how: "fit"
}, options);
var imgHTML = imgEl.get(0);
var imgRatio = imgHasDims ? imgHTML.width / imgHTML.height : imgEl.width() / imgEl.height();
var parentEl = options.parentEl || imgEl.parent();
var parentWidth = parentEl.innerWidth(),
parentHeight = parentEl.innerHeight(),
parentRatio = parentWidth / parentHeight;
var fitToHeight = (options.how === "fit" && imgRatio < parentRatio) || (options.how === "fill" && imgRatio > parentRatio);
imgEl.css(fitToHeight ? {
height: parentHeight,
width: parentHeight * imgRatio
} : {
width: parentWidth,
height: parentWidth / imgRatio
});
if (options.centerW) {
imgEl.css("margin-left", (parentWidth - imgEl.width()) / 2 + "px");
}
if (options.centerH) {
imgEl.css("margin-top", (parentHeight - imgEl.height()) / 2 + "px");
}
};
var loadImage = function (imgEl, callback, options) {
var imgHTML = imgEl.get(0);
if (imgHTML && imgHTML.width && imgHTML.height) {
callback(imgEl, true, options);
if(options.success) { options.success(imgEl); }
} else {
imgEl.load(function(ev) {
callback(imgEl, false, options);
if(options.success) { options.success(imgEl); }
}).error(function(ev) {
callback(imgEl, false, options);
if(options.error) { options.error(imgEl); }
});
}
};
var fitOrFill = function (how, containerEl, options) {
var imgEl = $(containerEl).find(' > img');
if(imgEl) {
loadImage(imgEl, scale, $.extend(options, {
how: how
}));
}
};
$.fn.fit = function (options) {
fitOrFill("fit", this, options);
};
$.fn.fill = function (options) {
fitOrFill("fill", this, options);
};
$.fn.fitAll = function (options) {
this.each(function() {
$(this).fit(options);
});
};
$.fn.fillAll = function (options) {
this.each(function() {
$(this).fill(options);
});
};
})(jQuery);