-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
82 lines (75 loc) · 1.88 KB
/
index.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
$.fn.popImg = function() {
var $layer = $("<div/>").css({
position: "fixed",
left: 0,
right: 0,
top: 0,
bottom: 0,
height: "100%",
width: "100%",
zIndex: 9999999,
background: "#000",
opacity: "0.6",
display: "none"
}).attr("data-id", "b_layer");
var cloneImg = function($node) {
var left = $node.offset().left;
var top = $node.offset().top;
var nodeW = $node.width();
var nodeH = $node.height();
return $node.clone().css({
position: "fixed",
width: nodeW,
height: nodeH,
left: left,
top: top,
zIndex: 10000000
});
};
var justifyImg = function($c) {
var dW = $(window).width();
var dH = $(window).height();
$c.css("cursor", "zoom-out").attr("data-b-img", 1);
var img = new Image();
img.onload = function(){
$c.stop().animate({
width: this.width,
height: this.height,
left: (dW - this.width) / 2,
top: (dH - this.height) / 2
}, 300);
};
img.src = $c.attr("src");
};
this.each(function(){
$(this).css("cursor", "zoom-in").on("click", function(){
var $b = $("body");
$layer.appendTo($b);
$layer.fadeIn(300);
var $c = cloneImg($(this));
$c.appendTo($b);
justifyImg($c);
});
});
var timer = null;
$(window).on("resize", function(){
$("img[data-b-img]").each(function(){
var $this = $(this);
timer && clearTimeout(timer);
timer = setTimeout(function(){
justifyImg($this);
}, 10);
});
});
$(window).on("click keydown", function(evt){
if(evt.type == "keydown" && evt.keyCode === 27) {
$layer.fadeOut(300);
$("img[data-b-img]").remove();
}
var $this = $(evt.target);
if($this.attr("data-id") == "b_layer" || $this.attr("data-b-img") == 1) {
$layer.fadeOut(300);
$("img[data-b-img]").remove();
}
});
};