enlarge-pic.js
点击右侧可以直接下载enlarge-pic.js
源文件👉🏻enlarge-pic.js
源码如下↓
javascript
function EnlargePic(args) {
var el = args["el"];
var type = args["type"];
var args = args["args"];
if (el && type) {
switch (type) {
case ("followmouse"):
FollowMouse(el, args);
break;
case ("screencenter"):
ScreenCenter(el, args);
break;
case ("click"):
Click(el, args);
break;
}
}
}
function FollowMouse(el, args) {
var x = 10;
var y = 20;
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).mouseover(function (e) {
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
'top': (window.innerHeight - e.pageY - y - height < 20 ? (window.innerHeight - height - 20) : (e.pageY + y)) + "px",
"left": (window.innerWidth - e.pageX - x - width < 20 ? (window.innerWidth - width - 20) : (e.pageX + x)) + "px"
}).show("fast");
}).mouseout(function () {
$("#imgBox").remove();
}).mousemove(function (e) {
$("#imgBox").css({
position: "absolute",
'top': (window.innerHeight - e.pageY - y - height < 20 ? (window.innerHeight - height - 20) : (e.pageY + y)) + "px",
"left": (window.innerWidth - e.pageX - x - width < 20 ? (window.innerWidth - width - 20) : (e.pageX + x)) + "px"
});
});
})
}
function ScreenCenter(el, args) {
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).mouseover(function (e) {
$("#imgBox").remove();
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
left: "50%",
top: "50%",
transform: "translate(-50%,-50%)"
}).show("fast");
}).mouseout(function () {
$("#imgBox").remove();
})
})
}
function Click(el, args) {
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).click(function (e) {
$("#imgBox").remove();
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div id='closeBox' title='关闭'>+</div><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
left: "50%",
top: "50%",
transform: "translate(-50%,-50%)"
}).show("fast");
$("#closeBox").css({
position: "absolute",
width: "36px",
height: "36px",
"font-size": "36px",
"font-weight": "600",
"color": "red",
"text-alight": "middle",
top: "-36px",
right: "-36px",
transform: "rotate(45deg)",
cursor: "pointer"
}).click(function () {
$("#imgBox").remove();
})
})
})
}
function EnlargePic(args) {
var el = args["el"];
var type = args["type"];
var args = args["args"];
if (el && type) {
switch (type) {
case ("followmouse"):
FollowMouse(el, args);
break;
case ("screencenter"):
ScreenCenter(el, args);
break;
case ("click"):
Click(el, args);
break;
}
}
}
function FollowMouse(el, args) {
var x = 10;
var y = 20;
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).mouseover(function (e) {
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
'top': (window.innerHeight - e.pageY - y - height < 20 ? (window.innerHeight - height - 20) : (e.pageY + y)) + "px",
"left": (window.innerWidth - e.pageX - x - width < 20 ? (window.innerWidth - width - 20) : (e.pageX + x)) + "px"
}).show("fast");
}).mouseout(function () {
$("#imgBox").remove();
}).mousemove(function (e) {
$("#imgBox").css({
position: "absolute",
'top': (window.innerHeight - e.pageY - y - height < 20 ? (window.innerHeight - height - 20) : (e.pageY + y)) + "px",
"left": (window.innerWidth - e.pageX - x - width < 20 ? (window.innerWidth - width - 20) : (e.pageX + x)) + "px"
});
});
})
}
function ScreenCenter(el, args) {
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).mouseover(function (e) {
$("#imgBox").remove();
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
left: "50%",
top: "50%",
transform: "translate(-50%,-50%)"
}).show("fast");
}).mouseout(function () {
$("#imgBox").remove();
})
})
}
function Click(el, args) {
var width = args ? args["width"] || 200 : 200;
var height = args ? args["height"] || 150 : 150;
el.each(function () {
$(this).click(function (e) {
$("#imgBox").remove();
var b = $(this).find("img").size() > 0 ? $(this).find("img") : $(this);
var imgUrl = b.attr("src") || b.text();
var imgBox = "<div id='imgBox'><img src='" + imgUrl + "' width='" + width + "' height='" + height + "'></img><div id='closeBox' title='关闭'>+</div><div>";
$("body").append(imgBox);
$("#imgBox").css({
position: "absolute",
left: "50%",
top: "50%",
transform: "translate(-50%,-50%)"
}).show("fast");
$("#closeBox").css({
position: "absolute",
width: "36px",
height: "36px",
"font-size": "36px",
"font-weight": "600",
"color": "red",
"text-alight": "middle",
top: "-36px",
right: "-36px",
transform: "rotate(45deg)",
cursor: "pointer"
}).click(function () {
$("#imgBox").remove();
})
})
})
}
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
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