-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagezoom.js
122 lines (110 loc) · 4.24 KB
/
imagezoom.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
/**
* ImageZoom Plugin
* http://0401morita.github.io/imagezoom-plugin
* MIT licensed
*
* Copyright (C) 2014 http://0401morita.github.io/imagezoom-plugin A project by Yosuke Morita
*/
(function($){
var defaults = {
cursorcolor:'255,255,255',
opacity:0.5,
cursor:'crosshair',
zindex:2147483647,
zoomviewsize:[480,395],
zoomviewposition:'right',
zoomviewmargin:10,
zoomviewborder:'none',
magnification:1.925
};
var imagezoomCursor,imagezoomView,settings,imageWidth,imageHeight,offset;
var methods = {
init : function(options){
$this = $(this),
imagezoomCursor = $('.imagezoom-cursor'),
imagezoomView = $('.imagezoom-view'),
$(document).on('mouseenter',$this.selector,function(e){
var data = $(this).data();
settings = $.extend({},defaults,options,data),
offset = $(this).offset(),
imageWidth = $(this).width(),
imageHeight = $(this).height(),
cursorSize = [(settings.zoomviewsize[0]/settings.magnification),(settings.zoomviewsize[1]/settings.magnification)];
if(data.imagezoom == true){
imageSrc = $(this).attr('src');
}else{
imageSrc = $(this).get(0).getAttribute('data-imagezoom');
}
var posX = e.pageX,posY = e.pageY,zoomViewPositionX;
$('body').prepend('<div class="imagezoom-cursor"> </div><div class="imagezoom-view"><img src="'+imageSrc+'"></div>');
if(settings.zoomviewposition == 'right'){
zoomViewPositionX = (offset.left+imageWidth+settings.zoomviewmargin);
}else{
zoomViewPositionX = (offset.left-imageWidth-settings.zoomviewmargin);
}
$(imagezoomView.selector).css({
'position':'absolute',
'left': zoomViewPositionX,
'top': offset.top,
'width': cursorSize[0]*settings.magnification,
'height': cursorSize[1]*settings.magnification,
'background':'#000',
'z-index':2147483647,
'overflow':'hidden',
'border': settings.zoomviewborder
});
$(imagezoomView.selector).children('img').css({
'position':'absolute',
'width': imageWidth*settings.magnification,
'height': imageHeight*settings.magnification,
});
$(imagezoomCursor.selector).css({
'position':'absolute',
'width':cursorSize[0],
'height':cursorSize[1],
'background-color':'rgb('+settings.cursorcolor+')',
'z-index':settings.zindex,
'opacity':settings.opacity,
'cursor':settings.cursor
});
$(imagezoomCursor.selector).css({'top':posY-(cursorSize[1]/2),'left':posX});
$(document).on('mousemove',document.body,methods.cursorPos);
});
},
cursorPos:function(e){
var posX = e.pageX,posY = e.pageY;
if(posY < offset.top || posX < offset.left || posY > (offset.top+imageHeight) || posX > (offset.left+imageWidth)){
$(imagezoomCursor.selector).remove();
$(imagezoomView.selector).remove();
return;
}
if(posX-(cursorSize[0]/2) < offset.left){
posX = offset.left+(cursorSize[0]/2);
}else if(posX+(cursorSize[0]/2) > offset.left+imageWidth){
posX = (offset.left+imageWidth)-(cursorSize[0]/2);
}
if(posY-(cursorSize[1]/2) < offset.top){
posY = offset.top+(cursorSize[1]/2);
}else if(posY+(cursorSize[1]/2) > offset.top+imageHeight){
posY = (offset.top+imageHeight)-(cursorSize[1]/2);
}
$(imagezoomCursor.selector).css({'top':posY-(cursorSize[1]/2),'left':posX-(cursorSize[0]/2)});
$(imagezoomView.selector).children('img').css({'top':((offset.top-posY)+(cursorSize[1]/2))*settings.magnification,'left':((offset.left-posX)+(cursorSize[0]/2))*settings.magnification});
$(imagezoomCursor.selector).mouseleave(function(){
$(this).remove();
});
}
};
$.fn.imageZoom = 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);
}
}
$(document).ready(function(){
$('[data-imagezoom]').imageZoom();
});
})(jQuery);