Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #286: Allow slide to close on videos #289

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ myGallery.setElements([...]);
| closeButton | boolean | `true` | Show or hide the close button. |
| touchNavigation | boolean | `true` | Enable or disable the touch navigation (swipe). |
| touchFollowAxis | boolean | `true` | Image follow axis when dragging on mobile. |
| touchVideoClosable | boolean | `false` | Enable or disable the closing of videos by swiping. |
| keyboardNavigation | boolean | `true` | Enable or disable the keyboard navigation. |
| closeOnOutsideClick | boolean | `true` | Close the lightbox when clicking outside the active slide. |
| startAt | number | `0` | Start lightbox at defined index. |
Expand Down
29 changes: 17 additions & 12 deletions dist/js/glightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@
var process = false;
var currentSlide = null;
var media = null;
var mediaImage = null;
var mediaElement = null;
var doingMove = false;
var initScale = 1;
var maxScale = 4.5;
Expand Down Expand Up @@ -1120,10 +1120,14 @@
currentSlide = instance.activeSlide;
media = currentSlide.querySelector('.gslide-media');
isInlined = currentSlide.querySelector('.gslide-inline');
mediaImage = null;
mediaElement = null;

if (hasClass(media, 'gslide-image')) {
mediaImage = media.querySelector('img');
mediaElement = media.querySelector('img');
}

if (instance.settings.touchVideoClosable && hasClass(media, 'gslide-video')) {
mediaElement = media.querySelector('.gvideo');
}

var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
Expand Down Expand Up @@ -1180,7 +1184,7 @@
vDistancePercent = vDistance * 100 / winHeight;
var opacity;

if (vSwipe && mediaImage) {
if (vSwipe && mediaElement) {
opacity = 1 - Math.abs(vDistance) / winHeight;
overlay.style.opacity = opacity;

Expand All @@ -1198,7 +1202,7 @@
}
}

if (!mediaImage) {
if (!mediaElement) {
return cssTransform(media, "translate3d(".concat(hDistancePercent, "%, 0, 0)"));
}

Expand All @@ -1220,7 +1224,7 @@
var v = Math.abs(parseInt(vDistancePercent));
var h = Math.abs(parseInt(hDistancePercent));

if (v > 29 && mediaImage) {
if (v > 29 && mediaElement) {
instance.close();
return;
}
Expand All @@ -1241,12 +1245,12 @@
initScale = currentScale ? currentScale : 1;
},
pinch: function pinch(evt) {
if (!mediaImage || doingMove) {
if (!mediaElement || doingMove) {
return false;
}

doingZoom = true;
mediaImage.scaleX = mediaImage.scaleY = initScale * evt.zoom;
mediaElement.scaleX = mediaElement.scaleY = initScale * evt.zoom;
var scale = initScale * evt.zoom;
imageZoomed = true;

Expand All @@ -1257,15 +1261,15 @@
lastZoomedPosX = null;
zoomedPosX = null;
zoomedPosY = null;
mediaImage.setAttribute('style', '');
mediaElement.setAttribute('style', '');
return;
}

if (scale > maxScale) {
scale = maxScale;
}

mediaImage.style.transform = "scale3d(".concat(scale, ", ").concat(scale, ", 1)");
mediaElement.style.transform = "scale3d(".concat(scale, ", ").concat(scale, ", 1)");
currentScale = scale;
},
pressMove: function pressMove(e) {
Expand All @@ -1289,7 +1293,7 @@
style += " scale3d(".concat(currentScale, ", ").concat(currentScale, ", 1)");
}

cssTransform(mediaImage, style);
cssTransform(mediaElement, style);
}
},
swipe: function swipe(evt) {
Expand Down Expand Up @@ -2435,7 +2439,7 @@
return Slide;
}();

var _version = '3.1.1';
var _version = '3.1.0';

var isMobile$1 = isMobile();

Expand Down Expand Up @@ -2474,6 +2478,7 @@
oneSlidePerOpen: false,
touchNavigation: true,
touchFollowAxis: true,
touchVideoClosable: false,
keyboardNavigation: true,
closeOnOutsideClick: true,
plugins: false,
Expand Down
2 changes: 1 addition & 1 deletion dist/js/glightbox.min.js

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions src/js/core/touch-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function touchNavigation(instance) {
let process = false;
let currentSlide = null;
let media = null;
let mediaImage = null;
let mediaElement = null;
let doingMove = false;
let initScale = 1;
let maxScale = 4.5;
Expand Down Expand Up @@ -97,9 +97,13 @@ export default function touchNavigation(instance) {
media = currentSlide.querySelector('.gslide-media');
isInlined = currentSlide.querySelector('.gslide-inline');

mediaImage = null;
mediaElement = null;
if (hasClass(media, 'gslide-image')) {
mediaImage = media.querySelector('img');
mediaElement = media.querySelector('img');
}

if (instance.settings.touchVideoClosable && hasClass(media, 'gslide-video')) {
mediaElement = media.querySelector('.gvideo');
}

const windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
Expand Down Expand Up @@ -154,7 +158,7 @@ export default function touchNavigation(instance) {
vDistancePercent = (vDistance * 100) / winHeight;

let opacity;
if (vSwipe && mediaImage) {
if (vSwipe && mediaElement) {
opacity = 1 - Math.abs(vDistance) / winHeight;
overlay.style.opacity = opacity;

Expand All @@ -171,7 +175,7 @@ export default function touchNavigation(instance) {
}
}

if (!mediaImage) {
if (!mediaElement) {
return cssTransform(media, `translate3d(${hDistancePercent}%, 0, 0)`);
}

Expand All @@ -190,7 +194,7 @@ export default function touchNavigation(instance) {
const v = Math.abs(parseInt(vDistancePercent));
const h = Math.abs(parseInt(hDistancePercent));

if (v > 29 && mediaImage) {
if (v > 29 && mediaElement) {
instance.close();
return;
}
Expand All @@ -210,12 +214,12 @@ export default function touchNavigation(instance) {
initScale = currentScale ? currentScale : 1;
},
pinch: (evt) => {
if (!mediaImage || doingMove) {
if (!mediaElement || doingMove) {
return false;
}

doingZoom = true;
mediaImage.scaleX = mediaImage.scaleY = initScale * evt.zoom;
mediaElement.scaleX = mediaElement.scaleY = initScale * evt.zoom;

let scale = initScale * evt.zoom;
imageZoomed = true;
Expand All @@ -227,15 +231,15 @@ export default function touchNavigation(instance) {
lastZoomedPosX = null;
zoomedPosX = null;
zoomedPosY = null;
mediaImage.setAttribute('style', '');
mediaElement.setAttribute('style', '');
return;
}
if (scale > maxScale) {
// max scale zoom
scale = maxScale;
}

mediaImage.style.transform = `scale3d(${scale}, ${scale}, 1)`;
mediaElement.style.transform = `scale3d(${scale}, ${scale}, 1)`;
currentScale = scale;
},
pressMove: (e) => {
Expand All @@ -257,7 +261,7 @@ export default function touchNavigation(instance) {
if (currentScale) {
style += ` scale3d(${currentScale}, ${currentScale}, 1)`;
}
cssTransform(mediaImage, style);
cssTransform(mediaElement, style);
}
},
swipe: (evt) => {
Expand Down
1 change: 1 addition & 0 deletions src/js/glightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const defaults = {
oneSlidePerOpen: false,
touchNavigation: true,
touchFollowAxis: true,
touchVideoClosable: false,
keyboardNavigation: true,
closeOnOutsideClick: true,
plugins: false,
Expand Down