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
| /**
* @author github.com@flymysql
*/
let container = document.documentElement || document.body;
let img, div, src, btnleft, btnright;
var imgid = 0;
let x, y, w, h, tx, ty, tw, th, ww, wh;
let closeMove = function () {
if (div == undefined) {
return false;
}
div.style.opacity = 0;
img.style.height = h + 'px';
img.style.width = w + 'px';
img.style.left = x + 'px';
img.style.top = y - container.scrollTop + 'px';
// 延迟移除 dom
setTimeout(function () {
div.remove();
img.remove();
btnright.remove();
btnleft.remove();
}, 100);
};
let closeFade = function () {
if (div == undefined) {
return false;
}
div.style.opacity = 0;
img.style.opacity = 0;
// 延迟移除 dom
setTimeout(function () {
div.remove();
img.remove();
btnright.remove();
btnleft.remove();
}, 100);
};
let style = function () {
btnleft.style.cssText = `
position:fixed;
border-radius: 50%;;
left:${x - 20}px;
top:${y - container.scrollTop + h / 2}px;
width:50px;
height:50px;
border: 0px;
background-color: rgba(200,200,200,0.8);
font-size: 20px;
z-index: 999999999;
transition:all .3s cubic-bezier(0.165, 0.84, 0.44, 1);
`;
btnright.style.cssText = `
position:fixed;
border-radius: 50%;
left:${x + w + 20}px;
top:${y - container.scrollTop + h / 2}px;
width:50px;
border: 0px;
height:50px;
font-size: 20px;
background-color: rgba(200,200,200,0.8);
z-index: 999999999;
transition:all .3s cubic-bezier(0.165, 0.84, 0.44, 1);
`;
btnleft.innerText = '<';
btnright.innerText = '>';
img.style.cssText = `
position:fixed;
border-radius: 12px;
left:${x}px;
top:${y - container.scrollTop}px;
width:${w}px;
height:${h}px;
z-index: 999999999;
transition:all .3s cubic-bezier(0.165, 0.84, 0.44, 1);
opacity:0;
`;
};
// 监听滚动关闭层
document.addEventListener('scroll', function () {
closeFade();
});
document.querySelectorAll('img').forEach((v) => {
if (v.parentNode.localName != 'a') {
v.id = imgid;
imgid++;
v.addEventListener('click', function (e) {
// 注册事件
// 记录小图的位置个大小
x = e.target.offsetLeft;
y = e.target.offsetTop;
w = e.target.offsetWidth;
h = e.target.offsetHeight;
src = e.target.src;
id = e.target.id;
// 创建遮罩层
div = document.createElement('div');
div.style.cssText = `
position:fixed;
left:0;
top:0;
bottom:0;
right:0;
background-color: rgba(25,25,25,0.8);
z-index:99999999;
transition:all .3s cubic-bezier(0.165, 0.84, 0.44, 1);
`;
document.body.appendChild(div);
setTimeout(function () {
div.style.opacity = 1;
}, 0);
// (此处可以加 loading)
// 创建副本
img = new Image();
btnright = document.createElement('button');
btnleft = document.createElement('button');
img.src = src;
style();
btnleft.onclick = function () {
if (id === 0) {
alert('已经是第一张了!');
return;
}
var left = document.getElementById(id - 1);
img.src = left.src;
x = left.offsetLeft;
y = left.offsetTop;
w = left.offsetWidth;
h = left.offsetHeight;
style();
id--;
};
btnright.onclick = function () {
id++;
if (id >= imgid) {
alert('已经是最后一张了!');
return;
}
var right = document.getElementById(id);
img.src = right.src;
x = right.offsetLeft;
y = right.offsetTop;
w = right.offsetWidth;
h = right.offsetHeight;
style();
};
img.onload = function () {
document.body.appendChild(img);
document.body.appendChild(btnright);
document.body.appendChild(btnleft);
// 浏览器宽高
wh = window.innerHeight;
ww = window.innerWidth;
// 目标宽高和坐标
if (w / h < ww / wh) {
th = wh - 80;
tw = ((w / h) * th) >> 0;
tx = (ww - tw) / 2;
ty = 40;
} else {
tw = ww * 0.8;
th = ((h / w) * tw) >> 0;
tx = ww * 0.1;
ty = (wh - th) / 2;
}
// 延迟写入否则不会有动画
setTimeout(function () {
img.style.opacity = 1;
img.style.height = th + 'px';
img.style.width = tw + 'px';
img.style.left = tx + 'px';
img.style.top = ty + 'px';
btnleft.style.left = tx - 90 + 'px';
btnleft.style.top = ty + th / 2 + 'px';
btnright.style.left = tx + tw + 40 + 'px';
btnright.style.top = ty + th / 2 + 'px';
// 点击隐藏
div.onclick = img.onclick = closeMove;
}, 10);
};
}); //end event
}
}); //end forEach
|