Skip to content

Commit a73f9b7

Browse files
committed
Minimalist Image Viewer Lightbox Plugin with jQuery
1 parent dc64fd8 commit a73f9b7

File tree

9 files changed

+149
-0
lines changed

9 files changed

+149
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
7+
# Standard to msysgit
8+
*.doc diff=astextplain
9+
*.DOC diff=astextplain
10+
*.docx diff=astextplain
11+
*.DOCX diff=astextplain
12+
*.dot diff=astextplain
13+
*.DOT diff=astextplain
14+
*.pdf diff=astextplain
15+
*.PDF diff=astextplain
16+
*.rtf diff=astextplain
17+
*.RTF diff=astextplain
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Windows image file caches
2+
Thumbs.db
3+
ehthumbs.db
4+
5+
# Folder config file
6+
Desktop.ini
7+
8+
# Recycle Bin used on file shares
9+
$RECYCLE.BIN/
10+
11+
# Windows Installer files
12+
*.cab
13+
*.msi
14+
*.msm
15+
*.msp
16+
17+
# Windows shortcuts
18+
*.lnk
19+
20+
# =========================
21+
# Operating System Files
22+
# =========================
23+
24+
# OSX
25+
# =========================
26+
27+
.DS_Store
28+
.AppleDouble
29+
.LSOverride
30+
31+
# Thumbnails
32+
._*
33+
34+
# Files that might appear on external disk
35+
.Spotlight-V100
36+
.Trashes
37+
38+
# Directories potentially created on remote AFP share
39+
.AppleDB
40+
.AppleDesktop
41+
Network Trash Folder
42+
Temporary Items
43+
.apdisk
44+
.idea
45+
.idea/*
120 KB
Loading
21.7 KB
Loading
96.5 KB
Loading
34.2 KB
Loading
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
imgViewer 1.0.0
3+
Licensed under the MIT license.
4+
https://github.com/VaJoy/imgViewer
5+
*/
6+
(function ($, window) {
7+
$.bindViewer = function (elm) {
8+
var $elm = $(elm),
9+
$win = $(window);
10+
$("html").css("minHeight", "100%");
11+
12+
if ($elm.data("bindCategory")) $(document).off("click", elm);
13+
else $elm.data("bindCategory", "bound");
14+
$(document).on("click", elm, viewImg);
15+
16+
function viewImg() {
17+
var $obj = $(this),
18+
src = $obj.attr("src"),
19+
win_h = window.innerHeight || document.documentElement.clientHeight,
20+
win_w = window.innerWidth || document.documentElement.clientWidth,
21+
sroll_t = $win.scrollTop(),
22+
sroll_l = $win.scrollLeft(),
23+
doc_h = Math.max($("body").height(), $("html").height()),
24+
$img = $("<img style='position:absolute;z-index:9999998;left:50%;border-radius:8px;opacity:0;' src='" + src + "' />"),
25+
$bg = $("<div style='position:absolute;z-index:9999997;top:0;left:0;width:100%;height:" + doc_h + "px;background:black;opacity:0.6;'></div>"),
26+
$close = $("<a title='关闭' style='position:absolute;z-index:9999999;left:50%;padding:11px 15px;cursor:pointer;background:black;color:white;border-radius:50%;font-family:Arial;font-size:14px;transition:background .5s;text-decoration:none;'>X</a>");
27+
$bg.appendTo("body").hide().fadeIn(200);
28+
$close.appendTo("body").hover(function () {
29+
$(this).css({"background": "#D90000", "text-decoration": "none"})
30+
}, function () {
31+
$(this).css("background", "black")
32+
}).hide();
33+
$img.appendTo("body").load(function () {
34+
var img_w = $(this).width(),
35+
img_h = $(this).height();
36+
if(win_h*0.8<img_h||win_w*0.8<img_w){ //处理图片大过屏幕的问题
37+
var win_scale = win_w/win_h,
38+
img_scale = img_w/img_h,
39+
temp = 0;
40+
if(win_scale>img_scale){ //由图片高度着手处理
41+
temp = img_h;
42+
img_h = win_h*0.8;
43+
img_w = img_w*img_h/temp;
44+
}else{ //由图片宽度着手处理
45+
temp = img_w;
46+
img_w = win_w*0.8;
47+
img_h = img_w*img_h/temp;
48+
}
49+
}
50+
$(this).css({"top": win_h / 2 + sroll_t, "margin-left": sroll_l - 50, "margin-top": "-50px", "width": "100px", "height": "100px"})
51+
.animate({"opacity": "1", "margin-left": -img_w / 2, "margin-top": -img_h / 2, "width": img_w, "height": img_h}, 300,
52+
function () {
53+
$close.css({"top": win_h / 2 + sroll_t, "margin-left": img_w / 2 - 20 + sroll_l, "margin-top": -10 - img_h / 2}).fadeIn(500);
54+
});
55+
$close.add($bg).click(function () {
56+
$img.add($bg).add($close).remove();
57+
$img = $bg = $close = null;
58+
})
59+
})
60+
}
61+
};
62+
63+
}(jQuery, window));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<script src="jq.js"></script>
6+
<script src="imgViewer.js"></script>
7+
<script>
8+
$.bindViewer(".viewer-item"); //支持绑定到动态生成的元素
9+
</script>
10+
<title>test</title>
11+
</head>
12+
<body>
13+
<span>222</span><p>213</p>
14+
<a href="javascript:void(0);"><img class="viewer-item" src="img/1.jpg" style="width:50px;height:50px;"/></a>
15+
<img class="viewer-item" src="img/2.jpg" style="width:50px;height:50px;cursor:pointer;"/>
16+
<img class="viewer-item" src="img/3.jpg" style="width:50px;height:50px;"/>
17+
<p>213</p><p>213</p><p>213</p><p>213</p>
18+
19+
</body>
20+
</html>

Minimalist-Image-Viewer-Lightbox-Plugin-with-jQuery/jq.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)