-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.photonav.js
360 lines (326 loc) · 8.96 KB
/
jquery.photonav.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* PhotoNavigation for WordPress "WP-PhotoNav"
*
* Version: 1.2.1
* Date: 16-08-14
* Author: Fabian Stanke
* Author URI: http://fmos.at
* License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Credits:
*
* PhotoNavigation the jQuery version
* A Javascript Module by Gaya Kessler
* Version 1.0
* Date: 09-04-09
*/
(function( $ ) {
// Extend the draggable plugin for 360 degree "infinite" dragging
$.ui.plugin.add( 'draggable', 'infinite', {
drag: function( event, ui ) {
if ( ui.position.left > -2 ) {
// right-dragging wrap around
ui.position.left -= ui.helper.wrapwidth;
} else if ( ui.position.left < -ui.helper.wrapwidth - 1 ) {
// left-dragging wrap around
ui.position.left += ui.helper.wrapwidth;
}
return true;
}
});
$.fn.photoNav = function( settings ) {
var defaults = {
mode: 'move',
popup: 'none',
animate: 'none',
speed: 1,
position: 'center',
label: 'none',
};
function PhotoNav( elem ) {
var self = this;
// Parse the position parameters
function parsePos( position, dimage, dcontainer ) {
var result;
switch ( position ) {
case 'center':
result = ( dcontainer - dimage ) / 2;
break;
case 'left':
case 'top':
result = ( dcontainer - dimage );
break;
case 'right':
case 'bottom':
result = 0;
break;
default:
result = parseFloat( position );
break;
}
return result;
}
function Container( container, photonav ) {
var content = container.find( '.content' ),
cw = container.width(),
ch = container.height(),
iw = photonav.image[0].scrollWidth,
ih = photonav.image[0].scrollHeight,
is_fullview = false,
leftStart = 0,
callback;
function initMove() {
container.mousemove(function( event ) {
var offset = $( this ).offset(),
curX = ( event.pageX - offset.left ) * ( 1 - iw / this.offsetWidth ),
curY = ( event.pageY - offset.top ) * ( 1 - ih / this.offsetHeight );
content.stop(); // stop animation
content.css({
left: Math.min( 0, curX ),
top: Math.min( 0, curY ),
});
});
}
function initDrag() {
content.draggable({
start: function( event, ui ) {
$( this ).stop(); // stop animation
},
drag: function( event, ui ) {
return ( ! is_fullview );
},
scroll: false
});
// Return a callback that gets called when the image is loaded
return function() {
container.mousedown(function(){
var co = container.offset();
content.draggable(
'option',
'containment',
[ co.left + cw - iw, co.top + ch - ih, co.left, co.top ] );
});
}
}
function initDrag360() {
content.draggable({
start: function( event, ui ) {
$( this ).stop(); // stop animation
ui.helper.wrapwidth = iw;
},
drag: function( event, ui ) {
return ( ! is_fullview );
},
scroll: false,
infinite: true // activate the plugin defined above
});
// Return a callback that gets called when the image is loaded
return function() {
content.css( 'width', iw + cw + 2 ); // for 360 mode, the dragable content is enlarged
container.mousedown(function(){
var co = container.offset();
content.draggable(
'option',
'containment',
[ , co.top + ch - ih, , co.top ] );
});
}
}
function initAutoScroll() {
var turnLeft = 'left' === photonav.config.animate,
turnLoop = 'drag360' === photonav.config.mode,
leftEnd; // Stop (or turn-around) position
function animate_loop( content ) {
content.css( 'left',
turnLeft ?
content.position().left + iw :
content.position().left - iw );
content.animate( {
left: leftEnd
},
10/photonav.config.speed * Math.abs( iw ),
'linear',
function() {
setTimeout( function() {
animate_loop( content );
},
1 );
} );
}
if ( turnLoop ) {
leftEnd = turnLeft ?
- iw :
0;
} else {
leftEnd = turnLeft ?
parsePos( 'left', iw, cw ) :
parsePos( 'right', iw, cw );
}
content.each(function() {
$( this ).animate( {
left: leftEnd
},
10/photonav.config.speed * Math.abs( leftEnd - leftStart ),
'linear',
function() {
if ( turnLoop ) {
animate_loop( $( this ) );
}
} );
});
}
function initZoom() {
var savePos = content.position();
container.mouseenter(function( event ) {
// Un-zoom, i.e. reset original size
photonav.image.css( 'width', 'auto' );
content.removeClass( 'zoomed' );
content.css({
left: savePos.left,
top: savePos.top
});
// Reset content width where necessary:
if ( undefined !== callback ) {
callback();
}
is_fullview = false;
});
container.mouseleave(function( event ) {
// Zoom out
savePos = content.position();
photonav.image.css( 'width', '100%' );
content.addClass( 'zoomed' );
content.css({
left: '',
top: '',
width: '',
});
is_fullview = true;
});
container.trigger( 'mouseleave' );
}
function setupDimensions() {
leftStart = parsePos( photonav.config.position, iw, cw );
if ( 0 === ch ) {
ch = ih; // ... otherwise use the image height
container.height( ch + 'px' );
}
content.css({
left: leftStart,
top: Math.min( 0, ( ch - ih ) / 2 ),
});
if ( callback !== undefined ) {
callback();
}
$( window ).resize( function() {
if ( ( container.width() !== cw ) || ( container.height() !== ch ) ) {
cw = container.width();
if ( ! is_fullview ) {
ch = container.height();
}
if ( content.position().left < cw - iw ) {
content.css( 'left', cw - iw );
}
if ( content.position().top < ch - ih ) {
content.css( 'top', ch - ih );
}
if ( callback !== undefined ) {
callback();
}
}
} );
}
function imageLoaded() {
iw = photonav.image[0].scrollWidth;
ih = photonav.image[0].scrollHeight;
cw = container.width();
ch = container.height();
setupDimensions();
var anim = photonav.config.animate;
if ( '1' === anim || 'right' === anim || 'left' === anim ) {
initAutoScroll();
} else if ( 'zoom' === anim ) {
initZoom();
}
}
// Call the appropriate init method above depending on the mode parameter.
switch ( photonav.config.mode ) {
case 'move':
callback = initMove();
break;
case 'drag':
callback = initDrag();
break;
case 'drag360':
callback = initDrag360();
break;
}
container
.find( '.image' )
.one( 'load', imageLoaded )
.each(function() {
if ( this.complete ) {
$( this ).load();
}
});
} // end function Container
// Initializes the ColorBox popup.
function initColorbox() {
var popup = elem.find( '.popup' ).first(),
container = popup.children( '.container' ).first(),
content = container.children( '.content' ).first();
self.image.colorbox({
maxWidth: '100%',
maxHeight: '100%',
width: self.image[0].scrollWidth,
inline: true,
href: popup,
onOpen: function() {
container.height( self.image[0].scrollHeight );
},
onComplete: function() {
var ph = popup.parent().innerHeight();
if ( ph < popup.height() ) {
container.height( ph );
}
var c_popup = new Container( container, self );
}
});
}
// Initialize on jQuery.ready event, do as much as possible to use the time while loading the image
this.init = function( config ) {
var inline = elem.children( '.container' );
self.config = config;
self.image = inline.find( '.image' ).first();
inline.css( 'display', 'block' ); // unhide (skip load optimization)
var c_inline = new Container( inline, self );
if ( 'colorbox' === config.popup ) {
if ( undefined !== $().colorbox ) {
initColorbox();
}
}
if ( 'none' !== config.label ) {
label = document.createElement( 'div' );
label.className = 'label';
inline.append( label );
elem.hover(function() {
$( label ).fadeTo( 'fast', 0.0 );
}, function() {
$( label ).fadeTo( 'fast', 1.0 );
});
}
if( 'number' !== typeof config.speed || config.speed <= 0) {
config.speed = 1;
}
};
};
this.each(function() {
var photonav = new PhotoNav( $( this ) );
var config = undefined !== settings ?
$.extend( {}, defaults, settings ) :
$.extend( {}, defaults )
photonav.init( config );
});
return this;
};
})( jQuery );