-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrollImg.js
More file actions
137 lines (134 loc) · 4.43 KB
/
Copy pathscrollImg.js
File metadata and controls
137 lines (134 loc) · 4.43 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
(function ($) {
$.fn.UI_scrollImg = function (settings, param) {
if (typeof settings == "string") {
var method = $.fn.UI_scrollImg.methods[settings];
if (method) {
return method(this, param);
}
}
settings = $.extend({}, defaults, settings);
return this.each(function () {
var data = $.data(this, "ui-scrollImg");
if (data) {
$.extend(data.settings, settings);
} else {
data = $.data(this, "ui-scrollImg", {settings: settings});
}
bindEvent(this);
});
};
$.fn.UI_scrollImg.methods = {
settings: function (jq) {
return $.data(jq[0], "ui-scrollImg").settings;
},
setCanMoved: function(jq) {
jq.each(function(){
setCanMoved(this);
});
}
};
var defaults = {
simpleZomm: false, //true=简单的放大缩小图片,不调整图片位置
scale: 0.02, //滚轮没动一次增加20%
canMoved: true, //是否能移动图片
MovedKey: "left", //移动图片的鼠标键 left/right
wrapHtml: '<div class="scroll-img-wrap" style="position: relative;overflow:hidden;border: 1px solid #0000FF;" ></div>'
};
function bindEvent(target) {
var settings = $(target).UI_scrollImg("settings");
$(target).unbind(".scrollImg");
function zoom(scale){
$(this).width($(this).width() * (1 + scale));
$(this).height($(this).height() * (1 + scale));
}
function placePosition(event){
var x = event.pageX ? event.pageX : event.clientX + document.body.scrollLeft - document.body.clientLeft,
y = event.pageY ? event.pageY : event.clientY + document.body.scrollTop - document.body.clientTop,
$div = $(this).parent(".scroll-img-wrap"),
pOffset = $div.offset(),
d_w = ($div.width()/2 - (x - pOffset.left))*settings.scale,
d_h = ($div.height()/2 - (y - pOffset.top))*settings.scale;
$(this).css({top: d_h + parseFloat($(this).css("top")), left: d_w + parseFloat($(this).css("left"))});
}
var removeMaxCss = false;
if(utils.browser.firefox){
$(target).bind("DOMMouseScroll.scrollImg", function(event){
//重置图片位置
if(!settings.simpleZomm)
placePosition.call(this, event.originalEvent);
var d = event.originalEvent.detail;
if(d > 0) {
zoom.call(this, -settings.scale);
}
else {
zoom.call(this, settings.scale);
}
if(!removeMaxCss){
$(this).css({"max-height": "none", "max-width": "none"});
removeMaxCss = true;
}
return false;
});
}else{
$(target).bind("mousewheel.scrollImg",function(event){
//重置图片位置
if(!settings.simpleZomm)
placePosition.call(this, event.originalEvent);
var d = event.originalEvent.wheelDelta;
if(d < 0){
zoom.call(this, -settings.scale);
}
else{
zoom.call(this, settings.scale);
}
if(!removeMaxCss){
$(this).css({"max-height": "none", "max-width": "none"});
removeMaxCss = true;
}
return false;
});
}
//图片能够被移动
if(!settings.simpleZomm && settings.canMoved){
setCanMoved(target);
}
}
function setCanMoved(target) {
var settings = $(target).UI_scrollImg("settings");
if(settings.wrapHtml){
var $wrap = $(settings.wrapHtml).css({width: $(target).width(), height: $(target).height()});
$(target).wrap($wrap).css({position: "absolute", left: "0px", top: "0px"});
}
var moving = false,
x,y,top,left;
$(target).bind("mousedown.scrollImg", function(event){
if(event.which == 1){
$(this).css("cursor", "move");
moving = true;
x = event.pageX ? event.pageX : event.clientX + document.body.scrollLeft - document.body.clientLeft;
y = event.pageY ? event.pageY : event.clientY + document.body.scrollTop - document.body.clientTop;
top = $(this).css("top");
left = $(this).css("left");
}
return false;
}).bind("mouseup.scrollImg", function(event){
if(event.which == 1){
$(this).css("cursor", "default");
moving = false;
}
return false;
}).bind("mouseleave.scrollImg", function(event){
//增加mouseleave事件是为了鼠标快速离开图片并不触发mouseup事件
$(this).css("cursor", "default");
moving = false;
return false;
}).bind("mousemove.scrollImg", function(event){
if(moving){
var tmp_x = event.pageX ? event.pageX : event.clientX + document.body.scrollLeft - document.body.clientLeft,
tmp_y = event.pageY ? event.pageY : event.clientY + document.body.scrollTop - document.body.clientTop;
$(this).css({top: (tmp_y - y) + parseFloat(top), left: (tmp_x - x) + parseFloat(left)});
}
return false;
});
}
})(jQuery);