Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Fix aspect ratio issues, add max-width/max-height options. #274

Merged
merged 3 commits into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
# Node
node_modules
.DS_Store

# Bower
bower_components

4 changes: 2 additions & 2 deletions dist/ekko-lightbox.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions dist/ekko-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var Lightbox = (function ($) {
var Default = {
title: '',
footer: '',
maxWidth: 9999,
maxHeight: 9999,
showArrows: true, //display the left / right arrows or not
wrapping: true, //if true, gallery loops infinitely
type: null, //force the lightbox into image / youtube mode. if null, or not image|youtube|vimeo; detect it
Expand All @@ -46,10 +48,8 @@ var Lightbox = (function ($) {
key: 'Default',

/**

Class properties:

_$element: null -> the <a> element currently being displayed
Class properties:
_$element: null -> the <a> element currently being displayed
_$modal: The bootstrap modal generated
_$modalDialog: The .modal-dialog
_$modalContent: The .modal-content
Expand Down Expand Up @@ -585,11 +585,19 @@ var Lightbox = (function ($) {
this._wantedWidth = width;
this._wantedHeight = height;

var imageAspecRatio = width / height;

// if width > the available space, scale down the expected width and height
var widthBorderAndPadding = this._padding.left + this._padding.right + this._border.left + this._border.right;
var maxWidth = Math.min(width + widthBorderAndPadding, this._config.doc.body.clientWidth);

// force 10px margin if window size > 575px
var addMargin = this._config.doc.body.clientWidth > 575 ? 20 : 0;
var discountMargin = this._config.doc.body.clientWidth > 575 ? 0 : 20;

var maxWidth = Math.min(width + widthBorderAndPadding, this._config.doc.body.clientWidth - addMargin, this._config.maxWidth);

if (width + widthBorderAndPadding > maxWidth) {
height = (maxWidth - widthBorderAndPadding) / width * height;
height = (maxWidth - widthBorderAndPadding - discountMargin) / imageAspecRatio;
width = maxWidth;
} else width = width + widthBorderAndPadding;

Expand All @@ -607,15 +615,15 @@ var Lightbox = (function ($) {
//calculated each time as resizing the window can cause them to change due to Bootstraps fluid margins
var margins = parseFloat(this._$modalDialog.css('margin-top')) + parseFloat(this._$modalDialog.css('margin-bottom'));

var maxHeight = Math.min(height, $(window).height() - borderPadding - margins - headerHeight - footerHeight);
var maxHeight = Math.min(height, $(window).height() - borderPadding - margins - headerHeight - footerHeight, this._config.maxHeight - borderPadding - headerHeight - footerHeight);

if (height > maxHeight) {
// if height > the available height, scale down the width
var factor = Math.min(maxHeight / height, 1);
width = Math.ceil(factor * width);
width = Math.ceil(maxHeight * imageAspecRatio) + widthBorderAndPadding;
}

this._$lightboxContainer.css('height', maxHeight);
this._$modalDialog.css('width', 'auto').css('maxWidth', width);
this._$modalDialog.css('flex', 1).css('maxWidth', width);

var modal = this._$modal.data('bs.modal');
if (modal) {
Expand Down
2 changes: 1 addition & 1 deletion dist/ekko-lightbox.js.map

Large diffs are not rendered by default.

Loading