forked from zhouatie/plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.js
198 lines (169 loc) · 8.03 KB
/
preview.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
(function(){
var util = {
css: function(elem,obj){
for(var i in obj){
elem.style[i] = obj[i];
}
},
addEvent: (function(window, undefined) {
var _eventCompat = function(event) {
var type = event.type;
if (type == 'DOMMouseScroll' || type == 'mousewheel') {
event.delta = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
}
//alert(event.delta);
if (event.srcElement && !event.target) {
event.target = event.srcElement;
}
if (!event.preventDefault && event.returnValue !== undefined) {
event.preventDefault = function() {
event.returnValue = false;
};
}
return event;
};
if (window.addEventListener) {
return function(el, type, fn, capture) {
if (type === "mousewheel" && document.mozFullScreen !== undefined) {
type = "DOMMouseScroll";
}
el.addEventListener(type, function(event) {
fn.call(this, _eventCompat(event));
}, capture || false);
};
} else if (window.attachEvent) {
return function(el, type, fn, capture) {
el.attachEvent("on" + type, function(event) {
event = event || window.event;
fn.call(el, _eventCompat(event));
});
};
}
return function() {};
})(window)
};
function Preview(opt){
var _this = this;
if(!opt.imgWrap || opt.imgWrap==""){
alert("请填写图片容器id");
return;
}
this.Opt = {
imgWrap: '',
beforeCreat: function(){},
afterCreat: function(){}
};
// 同步参数
for(var i in this.Opt){
if(opt[i]) this.Opt[i] = opt[i];
}
this.elemPreviewContain = document.getElementsByTagName("body")[0]; // 预览容器 预览默认展示在body内一层
this.elemContain = document.getElementById(this.Opt.imgWrap); // 被预览图片外面的容器
this.elemPreviewImg = null; // 预览的图片的载体
this.elemPreviewClose = null; // 关闭预览按钮
this.imgBg = null; // 预览最后面的黑色背景
this.moveObj = null // 实际被拖动的元素
this.init();
// 预览图片
this.LEFT = this.elemPreviewContain.offsetWidth/2; // 记录刚开始网页显示距离左边的位置
this.TOP = this.elemPreviewContain.offsetHeight/2; // 记录刚开始网页显示距离上边的位置
this.onOff = true; // 是否是点击事件的开关
this.startX = 0; // 存储按下鼠标时相对浏览器的位置X
this.startY = 0; // 存储按下鼠标时相对浏览器的位置Y
this.moveX = 0; // 移动的时候元素的left值
this.moveY = 0; // 移动的时候元素的top值
this._WIDTH = 0; // 预览图片原始载体宽度
this._HEIGHT = 0; // 预览图片原始载体高度
this.WIDTH = 0; // 预览图片载体宽度
this.HEIGHT = 0; // 预览图片载体高度
}
Preview.VERSION = '1.0.0';
var randomNum = new Date().getTime();
Preview.initParams = {
bgClass: "img_bgmask"
};
Preview.prototype = {
constructor: Preview,
init: function(){
var _this = this;
this.imgBg = document.createElement("div");
this.imgBg.className = 'img_bgmask';
util.css(this.imgBg,{position:"fixed",left:0,top:0,width:'100%',height:"100%",fontSize:0,background:'rgba(0,0,0,.6)',display:"none"});
this.imgBg.innerHTML = '<span style="position: absolute;top: 50px;right:50px;z-index: 10;height: 48px;width: 48px;background:red;cursor:pointer;border-radius:50%;" class="close_bgmask"></span><div style="position:absolute;left:50%;top:50%;" class="view_img_wrap"><img style="width:100%;height:100%;position: relative;z-index: -1;" src="" alt=""></div>';
this.elemPreviewContain.appendChild(this.imgBg);
this.elemPreviewImg = this.imgBg.getElementsByTagName("img")[0];
this.moveObj = this.elemPreviewImg.parentNode; // 被移动的元素,这里是图片载体的容器div
this.elemPreviewClose = this.imgBg.getElementsByTagName("span")[0];
this.elemContain.addEventListener("click",preview,false);
function preview(e){
e = e || window.event;
e.stopPropagation();
var target = e.target;
var tag = target.tagName.toLowerCase();
if(tag!=='img') return;
_this.elemPreviewImg.src = target.src;
util.css(_this.imgBg,{display:"block"});
_this._WIDTH = _this.WIDTH = _this.elemPreviewImg.offsetWidth;
_this._HEIGHT = _this.HEIGHT = _this.elemPreviewImg.offsetHeight;
util.css(_this.moveObj,{
width:_this.WIDTH+"px",
marginLeft:-_this.WIDTH/2+"px",
marginTop:-_this.HEIGHT/2+"px",
left:'50%',
top:'50%'
})
}
this.moveObj.addEventListener("mousedown",mousedown,false);
function mousedown(e){
e = e || window.event;
e.stopPropagation();
_this.onOff = true;
_this.startX = e.pageX;
_this.startY = e.pageY;
document.addEventListener("mousemove",mousemove,false);
function mousemove(ev){
_this.onOff=false;
_this.moveX = ev.pageX-_this.startX+_this.LEFT;
_this.moveY = ev.pageY-_this.startY+_this.TOP;
util.css(_this.moveObj,{
"left":_this.moveX+"px",
"top":_this.moveY+"px"
});
ev.preventDefault();
}
document.addEventListener("mouseup",mouseup,false);
function mouseup(ev){
document.removeEventListener("mousemove",mousemove,false);
document.removeEventListener("mouseup",mouseup,false);
if(_this.onOff) return false;
_this.LEFT = _this.moveX;
_this.TOP = _this.moveY;
}
e.preventDefault();
}
this.elemPreviewClose.onclick = function(){
_this.previewClose();
};
util.addEvent(this.imgBg, "mousewheel", function(event) {
this.WIDTH = _this.elemPreviewImg.offsetWidth;
if (event.delta > 0) {
util.css(_this.moveObj,{width:(this.WIDTH+20)+"px"});
}else {
util.css(_this.moveObj,{width:(this.WIDTH-20)+"px"});
}
this.HEIGHT = _this.elemPreviewImg.offsetHeight;
util.css(_this.moveObj,{
marginLeft:-this.WIDTH/2+"px",
marginTop:-this.HEIGHT/2+"px"
});
});
},
previewClose: function(){
this.LEFT = this.elemPreviewContain.offsetWidth/2; // 记录刚开始网页显示距离左边的位置
this.TOP = this.elemPreviewContain.offsetHeight/2; // 记录刚开始网页显示距离上边的位置
util.css(this.moveObj,{width:this._WIDTH+"px"});
util.css(this.imgBg,{display:"none"});
}
};
window.Preview = Preview;
})();