Skip to content

Commit 7b99f95

Browse files
committed
add download link
1 parent a534955 commit 7b99f95

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

js/lightbox.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
this.alwaysShowNavOnTouchDevices = false;
2222
this.wrapAround = false;
2323
}
24-
24+
2525
// Change to localize to non-english language
2626
LightboxOptions.prototype.albumLabel = function(curImageNum, albumSize) {
2727
return "Image " + curImageNum + " of " + albumSize;
@@ -58,8 +58,8 @@
5858
// Attach event handlers to the new DOM elements. click click click
5959
Lightbox.prototype.build = function() {
6060
var self = this;
61-
$("<div id='lightboxOverlay' class='lightboxOverlay'></div><div id='lightbox' class='lightbox'><div class='lb-outerContainer'><div class='lb-container'><img class='lb-image' src='' /><div class='lb-nav'><a class='lb-prev' href='' ></a><a class='lb-next' href='' ></a></div><div class='lb-loader'><a class='lb-cancel'></a></div></div></div><div class='lb-dataContainer'><div class='lb-data'><div class='lb-details'><span class='lb-caption'></span><span class='lb-number'></span></div><div class='lb-closeContainer'><a class='lb-close'></a></div></div></div></div>").appendTo($('body'));
62-
61+
$("<div id='lightboxOverlay' class='lightboxOverlay'></div><div id='lightbox' class='lightbox'><div class='lb-outerContainer'><div class='lb-container'><img class='lb-image' src='' /><div class='lb-nav'><a class='lb-prev' href='' ></a><a class='lb-next' href='' ></a></div><div class='lb-loader'><a class='lb-cancel'></a></div><a class='lb-download' href='' target='_blank'></a></div></div><div class='lb-dataContainer'><div class='lb-data'><div class='lb-details'><span class='lb-caption'></span><span class='lb-number'></span></div><div class='lb-closeContainer'><a class='lb-close'></a></div></div></div></div>").appendTo($('body'));
62+
6363
// Cache jQuery objects
6464
this.$lightbox = $('#lightbox');
6565
this.$overlay = $('#lightboxOverlay');
@@ -71,7 +71,7 @@
7171
this.containerRightPadding = parseInt(this.$container.css('padding-right'), 10);
7272
this.containerBottomPadding = parseInt(this.$container.css('padding-bottom'), 10);
7373
this.containerLeftPadding = parseInt(this.$container.css('padding-left'), 10);
74-
74+
7575
// Attach event handlers to the newly minted DOM elements
7676
this.$overlay.hide().on('click', function() {
7777
self.end();
@@ -114,6 +114,10 @@
114114
self.end();
115115
return false;
116116
});
117+
118+
this.$lightbox.find('.lb-download').on('click', function (e) {
119+
window.open(e.target.href)
120+
});
117121
};
118122

119123
// Show overlay and lightbox. If the image is part of a set, add siblings to album array.
@@ -166,7 +170,7 @@
166170
}
167171
}
168172
}
169-
173+
170174
// Position Lightbox
171175
var top = $window.scrollTop() + this.options.positionFromTop;
172176
var left = $window.scrollLeft();
@@ -184,6 +188,7 @@
184188

185189
this.disableKeyboardNav();
186190
var $image = this.$lightbox.find('.lb-image');
191+
var $downloadLink = this.$lightbox.find('.lb-download');
187192

188193
this.$overlay.fadeIn(this.options.fadeDuration);
189194

@@ -197,12 +202,13 @@
197202
preloader.onload = function() {
198203
var $preloader, imageHeight, imageWidth, maxImageHeight, maxImageWidth, windowHeight, windowWidth;
199204
$image.attr('src', self.album[imageNumber].link);
205+
$downloadLink.attr('href', self.album[imageNumber].link);
200206

201207
$preloader = $(preloader);
202208

203209
$image.width(preloader.width);
204210
$image.height(preloader.height);
205-
211+
206212
if (self.options.fitImagesInViewport) {
207213
// Fit image inside the viewport.
208214
// Take into account the border around the image and an additional 10px gutter on each side.
@@ -244,12 +250,12 @@
244250
// Animate the size of the lightbox to fit the image we are showing
245251
Lightbox.prototype.sizeContainer = function(imageWidth, imageHeight) {
246252
var self = this;
247-
253+
248254
var oldWidth = this.$outerContainer.outerWidth();
249255
var oldHeight = this.$outerContainer.outerHeight();
250256
var newWidth = imageWidth + this.containerLeftPadding + this.containerRightPadding;
251257
var newHeight = imageHeight + this.containerTopPadding + this.containerBottomPadding;
252-
258+
253259
function postResize() {
254260
self.$lightbox.find('.lb-dataContainer').width(newWidth);
255261
self.$lightbox.find('.lb-prevLink').height(newHeight);
@@ -273,7 +279,7 @@
273279
Lightbox.prototype.showImage = function() {
274280
this.$lightbox.find('.lb-loader').hide();
275281
this.$lightbox.find('.lb-image').fadeIn('slow');
276-
282+
277283
this.updateNav();
278284
this.updateDetails();
279285
this.preloadNeighboringImages();
@@ -330,15 +336,15 @@
330336
location.href = $(this).attr('href');
331337
});
332338
}
333-
339+
334340
if (this.album.length > 1 && this.options.showImageNumberLabel) {
335341
this.$lightbox.find('.lb-number').text(this.options.albumLabel(this.currentImageIndex + 1, this.album.length)).fadeIn('fast');
336342
} else {
337343
this.$lightbox.find('.lb-number').hide();
338344
}
339-
345+
340346
this.$outerContainer.removeClass('animating');
341-
347+
342348
this.$lightbox.find('.lb-dataContainer').fadeIn(this.options.resizeDuration, function() {
343349
return self.sizeOverlay();
344350
});

0 commit comments

Comments
 (0)