jquery.fakecrop.js
4.33 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***
* jquery.fakecrop.js
* Copyright (c) 2012 Vuong Nguyen
* http://vuongnguyen.com
*/
(function ($) {
var methods = {
ratio : function (args) {
var item = args.item,
settings = args.settings;
return { w : item.width()/settings.wrapperWidth,
h : item.height()/settings.wrapperHeight };
},
center : function (longVal, shortVal) {
return parseInt((longVal-shortVal)/2, 10);
},
scaleToFill : function (args) {
var item = args.item,
settings = args.settings,
ratio = settings.ratio,
width = item.width(),
height = item.height(),
offset = {top: 0, left: 0};
if (ratio.h > ratio.w) {
width = settings.wrapperWidth;
height = height / ratio.w;
if (settings.center) {
offset.top = methods.center(width, height);
}
} else {
height = settings.wrapperHeight;
width = width / ratio.h;
if (settings.center) {
offset.left = methods.center(height, width);
}
}
if (settings.center) {
args.wrapper.css('position', 'relative');
item.css({
'position' : 'absolute',
'top' : ['-', offset.top, 'px'].join(''),
'left' : offset.left + 'px'
});
}
return item.height(height).attr('height', height + 'px')
.width(width).attr('width', width + 'px');
},
scaleToFit : function (args) {
var item = args.item,
settings = args.settings,
ratio = settings.ratio,
width = item.width(),
height = item.height(),
offset = {top: 0, left: 0};
if (ratio.h > ratio.w) {
height = settings.wrapperHeight,
width = parseInt((item.width() * settings.wrapperHeight)/item.height(), 10);
if (settings.center) {
offset.left = methods.center(height, width);
}
} else {
height = parseInt((item.height() * settings.wrapperWidth)/item.width(), 10),
width = settings.wrapperWidth;
if (settings.center) {
offset.top = methods.center(width, height);
}
}
args.wrapper.css({
'width' : (settings.squareWidth ? settings.wrapperWidth : width) + 'px',
'height' : settings.wrapperHeight + 'px'
});
if (settings.center) {
args.wrapper.css('position', 'relative');
item.css({
'position' : 'absolute',
'top' : offset.top +'px',
'left' : offset.left + 'px'
});
}
return item.height(height).attr('height', height + 'px')
.width(width).attr('width', width + 'px');
},
init : function (options) {
var settings = $.extend({
wrapperSelector : null,
wrapperWidth : 100,
wrapperHeight : 100,
center : true,
fill : true,
initClass : 'fc-init',
doneEvent : 'fakedropdone',
squareWidth : true
}, options),
_init = function () {
var item = $(this),
wrapper = settings.wrapperSelector ? item.closest(settings.wrapperSelector) : item.parent(),
args = { item : item,
settings : settings,
wrapper : wrapper };
settings.ratio = methods.ratio(args);
if (settings.fill) {
wrapper.css({
'overflow' : 'hidden',
'width' : settings.wrapperWidth + 'px',
'height' : settings.wrapperHeight + 'px'
});
methods.scaleToFill(args);
} else {
methods.scaleToFit(args);
}
item.data('fc.settings', settings)
.addClass(settings.initClass) // Add class to container after initialization
.trigger(settings.doneEvent); // Publish an event to announce that fakecrop in initialized
},
images = this.filter('img'),
others = this.filter(':not(img)');
if (images.length) {
images.bind('load', function () {
_init.call(this);
this.style.display = 'inline';
}).each(function () {
// trick from paul irish's https://gist.github.com/797120/7176db676f1e0e20d7c23933f9fc655c2f120c58
if (this.complete || this.complete === undefined) {
var src = this.src;
this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
this.src = src;
this.style.display = 'none';
}
});
}
if (others.length) {
others.each(_init);
}
return this;
}
};
$.fn.fakecrop = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.fakecrop');
}
};
})(jQuery);