forked from Anima-t3d/photo-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-photo-resize.js
71 lines (61 loc) · 2.66 KB
/
jquery-photo-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/// <reference path="jquery-1.5.1.min.js" />
/*
* Adjust photo on browser window resize
*
* @example: $('selector').photoResize();
*
* @example:
$('selector').photoResize({
bottomSpacing:"Bottom Spacing adjustment"
});
*/
(function ($) {
$.fn.photoResize = function (options) {
var element = $(this),
defaults = {
bottomSpacing: 10,
rightSpacing: 20,
// remember initial picture size (used as maximum size)
unscaledHeight: $(element).height(),
unscaledWidth: $(element).width(),
upscaleImageWidth: true,
upscaleImageHeight: true
};
options = $.extend(defaults, options);
$(element).load(function () {
changeDimensions();
});
// the load event is a bit tricky -- it seems to not fire if
// the element has been loaded very fast... i.e. from the browser's cache.
// Therefore we force dimension change even before any load event has
// been received:
changeDimensions();
$(window).bind('resize', function () {
changeDimensions();
});
function changeDimensions() {
// again, we might have to load the picture, yet...
if ( options.unscaledHeight == 0 ) {
options.unscaledHeight = $(element).height();
options.unscaledWidth = $(element).width();
}
if ( options.unscaledHeight == 0 ) return;
var maxDisplayHeight = $(window).height() - $(element).offset().top - options.bottomSpacing;
var maxDisplayWidth = $(window).width() - $(element).offset().left - options.rightSpacing;
var desiredHeight = maxDisplayHeight < options.unscaledHeight || options.upscaleImageHeight ? maxDisplayHeight : options.unscaledHeight;
var desiredWidth = maxDisplayWidth < options.unscaledWidth || options.upscaleImageWidth ? maxDisplayWidth : options.unscaledWidth;
var currHeight = $(element).height();
var currWidth = $(element).width();
if ( currHeight != desiredHeight || currWidth != desiredWidth ) {
// keep aspect ratio
if ( desiredHeight / options.unscaledHeight <= desiredWidth / options.unscaledWidth ) {
$(element).height(desiredHeight);
$(element).width(options.unscaledWidth * desiredHeight / options.unscaledHeight);
} else {
$(element).height(options.unscaledHeight * desiredWidth / options.unscaledWidth);
$(element).width(desiredWidth);
}
}
}
};
}(jQuery));