Skip to content

Commit dba5b16

Browse files
authored
Create jquery.popup.lightbox.js
1 parent d623b7b commit dba5b16

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed

js/jquery.popup.lightbox.js

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/* Project: Popup Lightbox
2+
* Author: Asif Mughal
3+
* URL: www.codehim.com
4+
* License: MIT License
5+
* Copyright (c) 2019 - Asif Mughal
6+
*/
7+
/* File: jquery.popup.lightbox.js */
8+
(function ($) {
9+
$.fn.popupLightbox = function (options) {
10+
11+
var setting = $.extend({
12+
width: 560,
13+
height: 340,
14+
inAnimation: "ZoomIn",
15+
16+
}, options);
17+
18+
return this.each(function () {
19+
20+
var target = $(this);
21+
22+
var popupWindow = $section();
23+
24+
var closeBtn = $button();
25+
26+
var nextBtn = $button();
27+
28+
var prevBtn = $button();
29+
30+
var imgStat = $div();
31+
32+
var imgFig = $figure();
33+
34+
var capBar = $figcaption();
35+
36+
var imgs = $(target).find("img");
37+
38+
var totalImgs = imgs.length;
39+
40+
var imgNum = 0;
41+
var current, thisCaption;
42+
43+
44+
$(nextBtn).addClass("btn-next")
45+
.appendTo(popupWindow);
46+
47+
$(prevBtn).addClass("btn-prev")
48+
.appendTo(popupWindow);
49+
50+
$(closeBtn).addClass("btn-close")
51+
.appendTo(popupWindow)
52+
.html("\×");
53+
54+
$(imgStat).addClass("lightbox-status")
55+
.appendTo(popupWindow);
56+
57+
58+
$(imgFig).addClass("img-show")
59+
.appendTo(popupWindow);
60+
61+
$(popupWindow).addClass("lightbox animated faster " + setting.inAnimation).appendTo("body");
62+
63+
64+
//set up unique number for each image
65+
66+
for (var i = 0; i < imgs.length; i++) {
67+
68+
$(imgs).eq(i).attr({
69+
'data-num': i,
70+
'id': '#img' + i,
71+
});
72+
73+
74+
}
75+
76+
if ($(window).width() > 620) {
77+
78+
79+
$(popupWindow).css({
80+
'width': setting.width,
81+
'height': setting.height,
82+
'position': 'fixed',
83+
'top': '50%',
84+
'marginTop': -(setting.height / 2),
85+
'left': '50%',
86+
'marginLeft': -(setting.width / 2),
87+
'zIndex': '999',
88+
'overflow': 'hidden',
89+
90+
});
91+
92+
} else {
93+
$(popupWindow).css({
94+
'width': '100%',
95+
'height': '100%',
96+
'top': 0,
97+
'left': 0,
98+
99+
100+
});
101+
102+
103+
}
104+
105+
106+
$(capBar).addClass("img-caption animated fadeInUp");
107+
108+
109+
$(imgs).click(function () {
110+
111+
var thisImg = $(this).clone();
112+
var thisNum = $(this).attr("data-num") * 1;
113+
var $caption = $(this).attr('alt');
114+
if ($(this).prop('alt') == false) {
115+
$caption = "This image has no caption";
116+
}
117+
118+
119+
imgNum = thisNum;
120+
121+
if (thisNum + 1 == totalImgs) {
122+
$(nextBtn).hide();
123+
$(prevBtn).show();
124+
} else if (thisNum == 0) {
125+
$(prevBtn).hide();
126+
$(nextBtn).show();
127+
} else {
128+
$(nextBtn).show();
129+
$(prevBtn).show();
130+
}
131+
132+
$(imgStat).html(thisNum + 1 + " / " + totalImgs);
133+
134+
$(imgFig).html(thisImg)
135+
.parent().fadeIn();
136+
137+
$(capBar).html($caption).appendTo(imgFig);
138+
139+
});
140+
141+
142+
//Next image
143+
144+
$(nextBtn).click(function () {
145+
146+
var y = totalImgs - 1;
147+
148+
149+
if (imgNum < y) {
150+
imgNum += 1;
151+
}
152+
153+
if (imgNum + 1 == totalImgs) {
154+
$(nextBtn).hide();
155+
}
156+
157+
$(prevBtn).fadeIn();
158+
159+
160+
$(imgFig).find("img").animate({
161+
'left': '-100%',
162+
'opacity': 0,
163+
}, 200, function () {
164+
165+
$(imgFig).html($(imgs).eq(imgNum).clone());
166+
167+
current = $(imgFig).find("img");
168+
169+
thisCaption = $(current).attr("alt");
170+
171+
if ($(current).prop('alt') == false) {
172+
thisCaption = "This image has no caption";
173+
}
174+
175+
$(capBar).html(thisCaption).appendTo(imgFig);
176+
177+
$(imgStat).html(imgNum + 1 + " / " + totalImgs);
178+
179+
});
180+
181+
182+
});
183+
184+
//Previous image
185+
186+
$(prevBtn).click(function () {
187+
188+
189+
if (imgNum > 0) {
190+
191+
imgNum -= 1;
192+
}
193+
$(nextBtn).fadeIn();
194+
195+
196+
$(imgFig).find("img").animate({
197+
'right': '-100%',
198+
'opacity': 0,
199+
}, 200, function () {
200+
$(imgFig).html($(imgs).eq(imgNum).clone());
201+
202+
current = $(imgFig).find("img");
203+
204+
thisCaption = $(current).attr("alt");
205+
$(capBar).html(thisCaption).appendTo(imgFig);
206+
207+
208+
$(imgStat).html(imgNum + 1 + " / " + totalImgs);
209+
210+
});
211+
212+
if (imgNum == 0) {
213+
$(prevBtn).hide();
214+
}
215+
216+
217+
});
218+
219+
220+
function $div() {
221+
return document.createElement("div");
222+
}
223+
224+
function $button() {
225+
return document.createElement("button");
226+
227+
}
228+
229+
function $section() {
230+
231+
return document.createElement("section");
232+
233+
}
234+
235+
function $figure() {
236+
return document.createElement("figure");
237+
}
238+
239+
function $figcaption() {
240+
return document.createElement("figcaption");
241+
242+
}
243+
244+
245+
$(".btn-close").click(function () {
246+
$(this).parent().fadeOut();
247+
imgNum = 0;
248+
249+
250+
});
251+
252+
253+
});
254+
};
255+
256+
})(jQuery);

0 commit comments

Comments
 (0)