2
* Author: Constantin Boiangiu (services[at]constantinb.com)
3
* Homepage: http://www.constantinb.com/project/jquery-image-zoom-zoomit/
4
* Forum : http://www.constantinb.com/forums/forum/jquery-zoomit-plugin/
7
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
12
$.fn.jqZoomIt = function(options){
14
this.each(function() {
15
$(this).jqZoomIt(options);
21
mouseEvent : 'mouseenter', // click, mouseenter, mouseover
22
zoomAreaMove : 'mousemove', // drag or mousemove
23
zoomPosition : 'right', // left, right, top, bottom
25
zoomClass : 'zoomIt_zoomed', // additional styling on zoom container
26
zoomLoadingClass : 'zoomIt_loading', // css class to apply before the big image is fully loaded
27
zoomAreaClass : 'zoomIt_area', // additional styling on zoomed area
28
zoomAreaColor : '#FFF', // zoomed area bg color
30
zoomAreaOpacity : .5, // zoomed area opacity
31
zoomDistance : 10, // distance of the zoomer from the trigger
32
// full image multipliers based on small preview image size
33
multiplierX : 1.2, // how much of the big image width will be displayed
34
multiplierY : 1.2, // how much of the big image height will be displayed
40
is_touch : null // if left to null it will use an internal detection process. If bool, it will take the option and bypass internal functionality
43
var options = $.extend({}, defaults, options),
45
bigImg = $(this).attr('href'),
46
smallImg = $('img', this);
48
// if element doesn't have href attribute
49
// or small image isn't within main element,
51
if( '' == bigImg || 0 == smallImg.length ){
56
var initialize = function(){
57
// set the small image size
59
// prepare the preview wrapper element
62
var enterEvent = is_touch() ? 'touchstart' : ( 'click' == options.mouseEvent ? 'click' : 'mouseenter' ),
63
leaveEvent = is_touch() ? 'touchend' : 'mouseleave';
65
$(self).bind( enterEvent, function(event){
66
event.preventDefault();
68
options.zoom.call(self, {});
70
$(self).bind(leaveEvent, function(){
72
options.close.call(self, {});
75
$(window).resize( set_small_size );// reset positions on window resize
76
options.init.call(self, {});
80
var startZoom = function(){
81
var elems = get_preview_areas(), // returns object with keys zoomer and preview containing elements needed to create the zoom
82
sizes = get_small_size();
84
$(elems.zoomer).show().css({
88
// if no zoom is needed stop here
89
if( $.data( self, 'no_zoom') ){
93
// show and position preview area
94
$(elems.preview).show();
96
if( $.data( self, 'loaded' ) ){
100
// add loading class to big image container
101
$( elems.zoomer ).addClass( options.zoomLoadingClass );
102
// load the big image
103
var fullImg = $('<img />', {
106
// remove loader from zoom area
107
$( elems.zoomer ).removeClass( options.zoomLoadingClass );
110
'position' : 'absolute',
113
}).appendTo( $(elems.zoomer) );
115
$.data( self, 'loaded', true );
117
var fullWidth = fullImg.width(),
118
fullHeight = fullImg.height(),
119
ratioX = fullWidth / sizes.width,
120
ratioY = fullHeight / sizes.height,
121
dw = sizes.width / ratioX * ( options.multiplierX||1 ),
122
dh = sizes.height / ratioY * ( options.multiplierY||1 );
124
// if image doesn't need zooming according to multiplier, set full img container size to image size
125
if( options.multiplierX > ratioX && options.multiplierY > ratioY ){
126
$(elems.zoomer).css({
130
$.data( self, 'no_zoom', true );
134
if( 'drag' == options.zoomAreaMove && $.fn.draggable && !is_touch() ){
136
$(self).removeAttr('href');
138
$(elems.preview).draggable({
139
containment : 'parent',
140
drag: function(event, ui){
141
var pos = $(elems.preview).position(),
142
left = -(pos.left * ratioX),
143
top = -(pos.top * ratioY);
153
var moveEvent = is_touch() ? 'touchmove' : 'mousemove';
155
$(self).bind( moveEvent, function( e ){
156
// used to store current mouse position
159
// get position on touch
162
event = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
163
mouseX = event.pageX;
164
mouseY = event.pageY;
166
// get position on non-touch
171
var sizes = get_small_size(),
172
mX = mouseX - sizes.left - dw/2,
173
mY = mouseY - sizes.top - dh/2;
175
// horizontal left limit
176
if( mouseX < sizes.left + dw/2 ){
179
// vertical top limit
180
if( mouseY < sizes.top + dh/2 ){
183
// horizontal right limit
184
if( mouseX > ( sizes.left + sizes.width - dw/2 )){
185
mX = sizes.width - dw;
187
// vertical bottom limit
188
if( mouseY > ( sizes.top + sizes.height - dh/2 )){
189
mY = sizes.height - dh;
193
$(elems.preview).css({
200
'left' : -(mX * ratioX),
201
'top' : -(mY * ratioY)
206
// resize drag area that shows the position on small image
207
$(elems.preview).css({
214
var closeZoom = function(){
215
var elems = get_preview_areas();
216
$(elems.zoomer).css({
220
$(elems.preview).hide();
223
// set small image size
224
var set_small_size = function(){
226
var position = $(smallImg[0]).offset(),
228
'width' : $(smallImg[0]).outerWidth(),
229
'height' : $(smallImg[0]).outerHeight(),
230
'top' : position.top,
231
'left' : position.left,
232
'zTop' : position.top,
233
'zLeft' : position.left
235
// make some position calculations based on position option
236
switch( options.zoomPosition ){
239
data.zLeft += data.width + options.zoomDistance;
242
data.zLeft -= data.width * ( options.multiplierX || 1 ) + options.zoomDistance;
245
data.zTop += data.height + options.zoomDistance;
248
data.zTop -= data.height * ( options.multiplierY || 1 ) + options.zoomDistance;
252
$.data( smallImg[0], 'size', data );
256
// helper function to get small image size stored on element
257
var get_small_size = function(){
258
return $.data( smallImg[0], 'size' );
261
// creates an element that overlaps the image to highlight the area being zoomed
262
var add_preview_area = function(){
263
var dragger = $('<div />',{
264
'class': options.zoomAreaClass,
266
'background-color' : options.zoomAreaColor || '#999',
267
'opacity' : options.zoomAreaOpacity || .7,
269
'position' : 'absolute',
275
}).appendTo( $(self) );
277
var sizes = get_small_size();
279
var zoomer = $('<div />', {
280
'class' : options.zoomClass,
283
'position' : 'absolute',
285
'overflow' : 'hidden',
286
'width' : sizes.width * ( options.multiplierX || 1 ),
287
'height' : sizes.height * ( options.multiplierY || 1 ),
290
}).appendTo( $(document.body) );
296
$.data( smallImg[0], 'elems', data );
299
// helper function to return element that highlights the zoomed area
300
var get_preview_areas = function(){
301
return $.data( smallImg[0], 'elems');
305
* Verifies if browser has touch capabilities. Testing isn't bullet proof, you're encouraged
306
* to override the functionality by setting the parameter is_touch in options to a value determined
307
* by a third-party, more comprehensive browser testing script.
309
var is_touch = function(){
310
if( options.is_touch !== null ){
311
return options.is_touch;
313
return /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
b'\\ No newline at end of file'