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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// ==================================================
// GazeImg v1.4.1
//
// 采用 GPLv3 许可证供开源使用
// 或用于商业用途的 GazeImg 商业许可证
// 所有商业应用程序(包括您计划出售的网站,主题和应用程序)
// 都需要具有商业许可证。
//
// Licensed GPLv3 for open source use
// or GazeImg Commercial License for commercial use
//
// http://www.ganxiaozhe.com/p/gazeimg/
// Copyright 2020 Ganxiaozhe
//
//
// 根据 GPLv3 协议,下面是对此文件的修改说明
// + 添加对 ReferrerPolicy 的支持以便绕过同源检测 => Line: 294
// -- @kengwang at 2020/07/16
//
// ==================================================
;(function($,window,document){
'use strict';
if(!$) {return '缺少JQuery!';}
let g = {t:{}};
g.colors = [
"linear-gradient(to right, #C3E1CA, #E6E1BD)","linear-gradient(to right, #D4D3DD, #EFEFBB)",
"linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)","linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%)",
"linear-gradient(to bottom, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.15) 100%)",
"linear-gradient(to top, #c1dfc4 0%, #deecdd 100%)","linear-gradient(to top, #d5d4d0 0%, #d5d4d0 1%, #eeeeec 31%, #efeeec 75%, #e9e9e7 100%);"
];
function sizeHandle(opts){
if(typeof opts!='object'){return false;}
for (var val in opts){
opts[val] = parseInt( String(opts[val]).replace(/px/,'') );
}
opts.w <= 0 || isNaN(opts.w) && (opts.w=128);
opts.h <= 0 || isNaN(opts.h) && (opts.h=128);
if( opts.w<opts.rw && opts.h<opts.rh ){return opts;}
if(opts.w >= opts.rw && opts.w >= opts.h){
opts.h = opts.rw / opts.w * opts.h;
opts.w = opts.rw;
}
if(opts.h > opts.rh && opts.h > opts.w){
opts.w = opts.rh / opts.h * opts.w;
opts.h = opts.rh;
}
return opts;
}
$.fn.giLazy = function(data){
let t = {};
data || (data = {});
data.colors || (data.colors=["#000"]);
this.each(function(){
let timg = {};
timg.pw = $(this).parent().width();
timg.ph = $(this).parent().height();
t.gisize = 0;
t.gsrc = $(this).attr('data-gisrc').split(' ');
$(this).attr('data-gisrc',t.gsrc[0]);
if(t.gsrc[1] && t.gsrc[1].indexOf('size') > -1){
t.gisize = t.gsrc[1].replace(/size='(.*?)'/g,'$1').split(',');
var ti_s = sizeHandle({
w:t.gisize[0],h:t.gisize[1],
rw:timg.pw,rh:timg.ph
});
$(this).width(ti_s.w).height(ti_s.h);
}
// 是否有src数据,若无则替换为div蒙版
timg.src = $(this).attr('src') || false;
if(!timg.src){
// 获取图片具有的css信息即其父元素的长宽 来决定替代图片的遮罩层尺寸
timg.disp = $(this).css("display");
timg.msize = $(this).attr('gimask-size') || false;
if(timg.msize){
timg.msize = timg.msize.split(',');
} else {timg.msize = [];}
typeof timg.msize[0]=='undefined' && (timg.msize[0]=false);
typeof timg.msize[1]=='undefined' && (timg.msize[1]=false);
if(t.gisize!=0){
timg.w = t.gisize[0];
timg.h = t.gisize[1];
}
timg.w = timg.msize[0] || $(this).width();
timg.h = timg.msize[1] || $(this).height();
var ti_s = sizeHandle({
w:timg.w,h:timg.h,
rw:timg.pw,rh:timg.ph
});
timg.w = ti_s.w+'px';timg.h = ti_s.h+'px';
// 若图片display被设置为了inline 则将遮罩层设为inline-flex 否则继承图片display
timg.disp == "inline" ? timg.tdisp = "inline-flex" : timg.tdisp = timg.disp;
timg.tdisp = timg.tdisp.replace(/block/,'flex');
timg.css = "width:"+timg.w+";height:"+timg.h+";background:"+data.colors[Math.floor(Math.random()*data.colors.length)]+";";
timg.css += "display:"+timg.tdisp;
$(this).attr('data-disp',timg.disp).css({"display":"none"}).after('<div class="gi-darken" style="'+timg.css+'"><a href="//ganxiaozhe.com/p/gazeimg/" target="_blank"><div class="gilazy-loader gi-spin"></div></a></div>');
} else {
$(this).addClass('gi-darken');
}
if( $(this).attr('data-gazeimg')!=undefined ){$(this).giPrePop();}
}).attr('data-gi-init','');
setTimeout(()=>{$(window).scroll();},200);
return this;
};
$.fn.giPrePop = function(){
let t = {};
this.on("click",function(){
t.src = $(this).attr('src') || $(this).attr('data-gazeimg');
if(!t.src){return;}
t.stage = "<div class='gazeimg-container gi-fadeIn'><div class='gazeimg-bg'></div><div class='gazeimg-inner'><div class='gazeimg-nav'></div><div class='gazeimg-stage'><div class='gazeimg-content'><div class='loader gi-spin'></div></div></div></div><div class='gazeimg-thumbs'></div></div>";
$('body').append(t.stage).addClass('gazeimg-nobar');
g.t.gcc=0;
$('.gazeimg-container').on({
touchstart: function(e){
g.t.gcc=1;
let elem = e.originalEvent.target || e.target;
if($(elem).is('.gazeimg-content') || $(elem).is('.gazeimg-content *')){
return;
}
giStageClose();g.t.gcc=0;
},
mouseup: function(e){
if(g.t.gcc==1){return;}
let elem = e.target;
if($(elem).is('.gazeimg-content') || $(elem).is('.gazeimg-content *')){
return;
}
giStageClose();
}
});
t.i = new Image();
t.i.src = t.src;
if(t.i.complete){giShow(t.i);} else {
t.i.onload = function(){giShow(this);}
}
});
};
function giShow(img){
g.itemp = {};
g.itemp.rw = img.width;g.itemp.rh = img.height;
giSizeInit(img);
$('.gazeimg-content').html('<img src="'+img.src+'">');
$('.gazeimg-content').on({
mousedown: function(e){
if(e.which == 3){return;}
g.itemp.cd = true;g.itemp.clkt = 1;
$('.gazeimg-content img').css("cursor","grabbing");
g.t.dx = e.pageX,g.t.dy = e.pageY;
g.t.tran = $('.gazeimg-content').css('transform').replace(/[^0-9\-,]/g,'').split(',');
if(g.t.tran.length!=6) {g.t.tran = [0,0,0,0,0,0];}
},
mouseleave:function(){
g.itemp.cd = false;
},
mousemove: function(e){
if(!g.itemp.cd) {return;}
g.itemp.cd = 'moving';e.preventDefault();
g.t.x = parseInt(g.t.tran[4]) + e.pageX - g.t.dx;
g.t.y = parseInt(g.t.tran[5]) + e.pageY - g.t.dy;
$('.gazeimg-content').css({'transform':'translate('+g.t.x+'px,'+g.t.y+'px)'});
},
mouseup: function(e){
if(e.which == 3 || g.itemp.clkt == 2){return;}
if(g.itemp.cd!='moving'){giShowEnd();}
g.itemp.cd = false;$('.gazeimg-content img').css("cursor","grab");
},
touchstart:function(e){
g.itemp.cd = true;g.itemp.clkt++;
g.t.dx = e.originalEvent.changedTouches[0].pageX;
g.t.dy = e.originalEvent.changedTouches[0].pageY;
g.t.tran = $('.gazeimg-content').css('transform').replace(/[^0-9\-,]/g,'').split(',');
if(g.t.tran.length!=6) {g.t.tran = [0,0,0,0,0,0];}
},
touchmove:function(e) {
if(!g.itemp.cd) {return;}
g.itemp.cd = 'moving';e.preventDefault();
g.t.x = parseInt(g.t.tran[4]) + e.originalEvent.changedTouches[0].pageX - g.t.dx;
g.t.y = parseInt(g.t.tran[5]) + e.originalEvent.changedTouches[0].pageY - g.t.dy;
$('.gazeimg-content').css({'transform':'translate('+g.t.x+'px,'+g.t.y+'px)'});
},
touchend:function(){
if(g.itemp.cd!='moving' && g.itemp.clkt==1){giShowEnd();}
g.itemp.cd = false;
}
});
function giShowEnd(){
let t = {tgt:0};
t.gzoom = $('.gazeimg-content').attr('data-gzoom') || '';
if(t.gzoom=='out') {
$('.gazeimg-content').css({'transform':'translate('+g.itemp.left+'px,'+g.itemp.top+'px) scale(1,1)'});
$('.gazeimg-content').height(g.itemp.h).width(g.itemp.w).attr({'data-rlong':'y','data-gzoom':'in'});
t.tgt++;
}
if( (g.itemp.rw > g.ww || g.itemp.rh > g.wh) && t.gzoom!='out' ){
$('.gazeimg-content').css({'transform':'translate('+0+'px,'+0+'px) scale(1,1)'});
$('.gazeimg-content').height(g.itemp.rh).width(g.itemp.rw).attr({'data-rlong':'n','data-gzoom':'out'});
t.tgt++;
}
if(t.tgt==0){giStageClose();}
}
}
function giSizeInit(img){
if(!g.itemp) {return false;}
g.ww = window.innerWidth;g.wh = window.innerHeight;
g.sw = $('.gazeimg-stage').width();g.sh = $('.gazeimg-stage').height();
if(!img) {img = $('.gazeimg-content img')[0];}
g.itemp = giSizeProcess(img);
$('.gazeimg-content').height(g.itemp.h).width(g.itemp.w);
$('.gazeimg-content').css({'transform':'translate('+g.itemp.left+'px,'+g.itemp.top+'px) scale(1,1)'});
}
function giStageClose(){
$('.gazeimg-content').height(0).width(0);
$('.gazeimg-container').fadeOut(500,function(){
$(this).remove();
$('body').removeClass('gazeimg-nobar');
});
}
function giSizeProcess(img){
let t = {};
g.sw = $('.gazeimg-stage').width();g.sh = $('.gazeimg-stage').height();
t = sizeHandle({
w:g.itemp.rw,h:g.itemp.rh,
rw:g.sw,rh:g.sh
});
t.rw = g.itemp.rw,t.rh = g.itemp.rh;
if(t.w > g.sw) {
t.w = g.sw;t.h = t.w / t.rw * t.rh;
}
if(t.h > g.sh) {
t.h = g.sh;t.w = t.h / t.rh * t.rw;
}
// 获取图片相对于屏幕的各数据
t.left = parseInt((g.ww - t.w) / 2);t.top = parseInt((g.wh - t.h) / 2);
return t;
}
$(window).resize(function(){giSizeInit();});
$('img[data-gisrc]:not([data-gi-init])').giLazy({'colors':g.colors});
})(jQuery,window,document);
$(window).scroll(function(){
let t = {};
t.deh = document.documentElement.clientHeight;
$('img[data-gi-init]').each(function(){
if( $(this).next(".gi-darken").length > 0 ){
t.ietop = $(this).next(".gi-darken")[0].getBoundingClientRect().top;
} else {
t.ietop = $(this)[0].getBoundingClientRect().top;
}
if( t.ietop < t.deh ){
let i = new Image();
i.src = $(this).attr('data-gisrc');
if (this.referrerPolicy==="no-referrer"){
i.referrerPolicy="no-referrer";
}
i.obj = $(this);
if (i.complete) {icomplete(i.obj);return;};
i.onerror = function(){
if( i.obj.next(".gi-darken").length > 0 ){
i.obj.next(".gi-darken").html('<a href="'+i.src+'" target="_blank">图片 '+i.src+' 加载失败;</a>').css({'background':'#000'});
}
i.obj.removeAttr('data-gisrc').removeAttr('data-gi-init');
i = i.onload = i.onerror = null;
};
i.onload = function(){
icomplete(i.obj);i = i.onload = i.onerror = null;
};
}
});
function icomplete(obj){
obj.next(".gi-darken").remove();
obj.attr({"src":obj.attr('data-gisrc')}).css({"display":obj.attr('data-disp')}).addClass('gi-fadeIn');
obj.removeAttr('data-gisrc').removeAttr('data-gi-init');
setTimeout(()=>{
obj.css({filter:"brightness(1)"});
},200);
}
});
|