Skip to content

Commit 049cdbe

Browse files
committed
1.6.0 - Added double tap
Added double tap to zoom in/out on touch devices
1 parent 7a599e2 commit 049cdbe

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A simple and basic Javascript lightbox.
1515
* Multiple galleries. Custom options for each.
1616
* Keyboard arrow keys to navigate and escape to close supported.
1717
* Swipe gestures supported on touch devices.
18-
* Spread/Pinch to Zoom In/Out supported on touch devices.
18+
* Spread/Pinch or Double Tap to Zoom In/Out on touch devices.
1919
* MouseWheel for next/prev images.
2020
* Ctrl+MouseWheel to Zoom In/Out.
2121
* SVG Icons.

demo/js/halkaBox.min.js

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

dist/halkaBox.min.js

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "halkabox",
3-
"version": "1.5.1",
3+
"version": "1.6.0",
44
"author": "Ahmed Noor",
55
"description": "A simple and basic Javascript lightbox",
66
"license": "MIT",

src/halkaBox.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
halkaBox.js , url: https://github.com/ahmednooor/halkaBox.js
3-
Version: 1.5.1
3+
Version: 1.6.0
44
Auther: Ahmed Noor , url: https://github.com/ahmednooor
55
License: MIT , url: https://opensource.org/licenses/MIT
66
*/
@@ -74,6 +74,12 @@ var halkaBox = (function () {
7474
touch2PositionY,
7575
isZoomed = false,
7676
zoomPercentage = 100,
77+
doubleTapDelay = 300,
78+
doubleTapFlags = {
79+
isSingleTapped: false,
80+
whenSingleTapped: null,
81+
isDoubleTapped: false,
82+
},
7783
option,
7884
customOptions = {},
7985
controlsHidden = true,
@@ -384,6 +390,7 @@ var halkaBox = (function () {
384390

385391
// function for closing popup by clicking on empty space
386392
function bgClickClose(event) {
393+
if (isZoomed) return;
387394
// to prevent bubbling
388395
event.stopPropagation();
389396
event.preventDefault();
@@ -412,12 +419,13 @@ var halkaBox = (function () {
412419
// zoom related functions
413420
function resetZoom(img) {
414421
img.removeAttribute("style");
415-
img.style.transition = "all 150ms ease-out";
422+
img.style.transition = "all 150ms ease-in";
416423
zoomPercentage = 100;
417-
isZoomed = false;
418424
window.setTimeout(function() {
425+
isZoomed = false;
426+
img.style.transition = "";
419427
img.removeAttribute("style");
420-
}, 160);
428+
}, 200);
421429
}
422430
function checkCorners(img) {
423431
var imgComputedWidth = parseInt(window.getComputedStyle(img,null).getPropertyValue("width"));
@@ -465,16 +473,18 @@ var halkaBox = (function () {
465473

466474
if (imgComputedWidth <= window.innerWidth && imgComputedHeight <= window.innerHeight) {
467475
resetZoom(img);
476+
return;
468477
}
469478

470479
window.setTimeout(function() {
471480
img.style.transition = "";
472-
}, 160);
481+
}, 200);
473482
}
474483
function moveImage(x, y, img) {
475484
var numericLeft = window.getComputedStyle(img,null).getPropertyValue("left");
476485
var numericTop = window.getComputedStyle(img,null).getPropertyValue("top");
477486

487+
img.style.transition = "";
478488
img.style.position = "absolute";
479489

480490
img.style.top = (parseInt(numericTop) + (y)) + "px";
@@ -485,6 +495,9 @@ var halkaBox = (function () {
485495
function zoomImage(img) {
486496
if (!customOptions.isZoomable) return
487497

498+
hideCaption();
499+
hideControls();
500+
488501
var imgComputedWidth = parseInt(window.getComputedStyle(img, null).getPropertyValue("width"));
489502
var imgComputedHeight = parseInt(window.getComputedStyle(img, null).getPropertyValue("height"));
490503

@@ -572,8 +585,38 @@ var halkaBox = (function () {
572585
}
573586
}
574587

575-
// touch handlers for swipe and zoom
588+
function resetDoubleTapFlags() {
589+
doubleTapFlags.isSingleTapped = false;
590+
doubleTapFlags.whenSingleTapped = null;
591+
doubleTapFlags.isDoubleTapped = false;
592+
}
593+
594+
// touch handlers
576595
function touchStart(event) {
596+
if (doubleTapFlags.isSingleTapped && event.touches.length === 1 && new Date().getTime() - doubleTapFlags.whenSingleTapped < doubleTapDelay) {
597+
doubleTapFlags.isDoubleTapped = true;
598+
if (isZoomed === false) {
599+
zoomPercentage = 200;
600+
imageObjects[curIndex].getElementsByTagName("img")[0]
601+
.style.transition = "all 150ms ease-out";
602+
window.setTimeout(function() {
603+
imageObjects[curIndex].getElementsByTagName("img")[0]
604+
.style.transition = "";
605+
}, 200);
606+
zoomImage(imageObjects[curIndex].getElementsByTagName("img")[0]);
607+
} else {
608+
resetZoom(imageObjects[curIndex].getElementsByTagName("img")[0]);
609+
}
610+
resetDoubleTapFlags();
611+
return;
612+
}
613+
if (!doubleTapFlags.isSingleTapped && event.touches.length === 1) {
614+
doubleTapFlags.isSingleTapped = true;
615+
doubleTapFlags.whenSingleTapped = new Date().getTime();
616+
window.setTimeout(function() {
617+
resetDoubleTapFlags();
618+
}, doubleTapDelay);
619+
}
577620
// if orientation has been changed then set orientPortrait to false or vice versa and set viewort equal to new window.innerWidth
578621
if ((window.innerWidth < window.innerHeight) !== orientPortrait) {
579622
orientPortrait = orientPortrait === true ? false : true;
@@ -585,12 +628,15 @@ var halkaBox = (function () {
585628
if (event.touches.length > 1) {
586629
touch2PositionX = event.touches[1].clientX;
587630
touch2PositionY = event.touches[1].clientY;
588-
} else {
589-
return false;
590631
}
591632
}
592633
function touchMove(event) {
593634
event.preventDefault();
635+
636+
if (doubleTapFlags.isDoubleTapped) return;
637+
638+
if (touchEnabled) resetDoubleTapFlags();
639+
594640
var touch = event.touches[0] || event.changedTouches[0],
595641
touches = event.touches.length;
596642
// to check if touchEnabled is false, touches are not two and browser is not zoomed in
@@ -634,13 +680,17 @@ var halkaBox = (function () {
634680
touchPositionY = event.touches[0].clientY;
635681

636682
moveImage(touchDiffX, touchDiffY, imageObjects[curIndex].getElementsByTagName("img")[0]);
637-
} else {
638-
return false;
639683
}
640684
}
641685
function touchEnd() {
642686
touchEnabled = false;
643-
checkCorners(imageObjects[curIndex].getElementsByTagName("img")[0]);
687+
if (isZoomed) {
688+
checkCorners(imageObjects[curIndex].getElementsByTagName("img")[0]);
689+
return;
690+
}
691+
window.setTimeout(function() {
692+
checkCorners(imageObjects[curIndex].getElementsByTagName("img")[0]);
693+
}, doubleTapDelay);
644694
}
645695

646696
// functions to hide controls / captions

0 commit comments

Comments
 (0)