-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.iror.js
239 lines (168 loc) · 5.86 KB
/
jquery.iror.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* by Pablo Balderas
*
* version 0.6
* http://www.softtek.com
*
* == 0.6
* - can now include a callback in the options, it will run after the bg loads
* == 0.5
* - added default options
* == 0.4
* - added behaviors:
* - behavior 1: image dimension = window dimension (AKA: fillwindow)
* - behavior 2: image dimension = content dimension (AKA: fillcontent)
* == 0.3
* - added focal point functionality
* == 0.2
* - prepend, dont replace parent's html
* - position: absolute the image, so it can play with same-level siblings
* == 0.1
* - replace parent's content with image
* - position: relative the image, parent is position: absolute
*/
;
(function($){
$.fn.iror = function( options ){
var defaults = {
image_src : "",
focalpointx : "left-0",
focalpointy : "top-0",
behavior : "fillwindow",
runafterload: null
};
var opts = $.extend( {}, defaults, options );
var parent = this;
var bg = new Image();
var bgratio = 0;
var winratio = 0;
var contentratio;
var focaldirectionx = opts.focalpointx.split("-")[0]; // top-1 , left-3 , bottom-5 , right-2
var focalmagnitudex = opts.focalpointx.split("-")[1];
var focaldirectiony = opts.focalpointy.split("-")[0];
var focalmagnitudey = opts.focalpointy.split("-")[1];
var focalpoint_settings = {
size_x:10,
size_y:10
};
function calculateFocalPointVerticalFix(remainder){
var result = remainder;
var percent = (1 / (focalpoint_settings.size_y / 2)) * (focalmagnitudey);
if ( focaldirectiony === "bottom"){
result = remainder + (remainder * percent);
} else if ( focaldirectiony === "top"){
result = remainder - (remainder * percent);
}
return result;
}
function calculateFocalPointHorizontalFix(remainder){
var result = remainder;
var percent = (1 / (focalpoint_settings.size_x / 2)) * (focalmagnitudex);
if ( focaldirectionx === "right"){
result = remainder + (remainder * percent);
} else if ( focaldirectionx === "left"){
result = remainder - (remainder * percent);
}
return result;
}
function cropHorizontallyByWindow(){
// set the image's height to the window's height
$( bg ).height( $( window ).height() );
// fix the width accordingly, keep aspect ratio
var newwidth = $(bg).height() / bgratio;
$( bg ).width( newwidth );
// apply focal point
var remainder = (bg.width / 2) - ($( window ).width() / 2);
var bgxfix = calculateFocalPointHorizontalFix(remainder);
$( bg ).css("left", "-"+bgxfix+"px");
// un-center the image in the y axis
$( bg ).css("top", "0px");
}
function cropVerticallyByWindow(){
// set the image's width to the window's width
$( bg ).width( $( window ).width() );
// fix the height accordingly, keep aspect ratio
var newheight = $(bg).width() * bgratio;
$( bg ).height( newheight );
// apply focal point
var remainder = (bg.height / 2) - ($( window ).height() / 2);
var bgyfix = calculateFocalPointVerticalFix(remainder);
$( bg ).css("top", "-"+bgyfix+"px");
// un-center the image in the x axis
$( bg ).css("left", "0px");
}
function cropHorizontallyByContent(){
// set the image's height to the window's height
$( bg ).height( $( parent ).height() );
// fix the width accordingly, keep aspect ratio
var newwidth = $(bg).height() / bgratio;
$( bg ).width( newwidth );
// apply focal point
var remainder = (bg.width / 2) - ($( parent ).width() / 2);
var bgxfix = calculateFocalPointHorizontalFix(remainder);
$( bg ).css("left", "-"+bgxfix+"px");
// un-center the image in the y axis
$( bg ).css("top", "0px");
}
function cropVerticallyByContent(){
// set the image's width to the window's width
$( bg ).width( $( parent ).width() );
// fix the height accordingly, keep aspect ratio
var newheight = $(bg).width() * bgratio;
$( bg ).height( newheight );
// apply focal point
var remainder = (bg.height / 2) - ($( parent ).height() / 2);
var bgyfix = calculateFocalPointVerticalFix(remainder);
$( bg ).css("top", "-"+bgyfix+"px");
// un-center the image in the x axis
$( bg ).css("left", "0px");
}
function checkForCropByWindow(){
winratio = $( window ).height() / $( window ).width();
if (winratio >= bgratio){
cropHorizontallyByWindow();
} else if (winratio < bgratio){
cropVerticallyByWindow();
}
}
function checkForCropByContent(){
contentratio = $(parent).height() / $(parent).width();
if (contentratio >= bgratio){
cropHorizontallyByContent();
} else if (contentratio < bgratio){
cropVerticallyByContent();
}
}
function setParentHeightToWindow(){
$(parent).height( $( window ).height() );
}
function behave(){
if (opts.behavior === "fillwindow"){
// always keep the parent's height equal to window's height
setParentHeightToWindow();
checkForCropByWindow();
} else if (opts.behavior === "fillcontent"){
checkForCropByContent();
} else {
// default behavior: fillwindow
setParentHeightToWindow();
checkForCropByWindow();
}
}
$(bg).load(function(){
bgratio = bg.height / bg.width;
$( window ).resize(function() {
behave();
});
// first time run
behave();
// run the callback at this point
if (typeof opts.runafterload === "function"){
opts.runafterload();
}
});
bg.src = ""+opts.image_src;
$(bg).css("position","absolute");
$(parent).prepend(bg);
}; // end plugin
})(jQuery);