1
1
/*
2
2
halkaBox.js , url: https://github.com/ahmednooor/halkaBox.js
3
- Version: 1.5.1
3
+ Version: 1.6.0
4
4
Auther: Ahmed Noor , url: https://github.com/ahmednooor
5
5
License: MIT , url: https://opensource.org/licenses/MIT
6
6
*/
@@ -74,6 +74,12 @@ var halkaBox = (function () {
74
74
touch2PositionY ,
75
75
isZoomed = false ,
76
76
zoomPercentage = 100 ,
77
+ doubleTapDelay = 300 ,
78
+ doubleTapFlags = {
79
+ isSingleTapped : false ,
80
+ whenSingleTapped : null ,
81
+ isDoubleTapped : false ,
82
+ } ,
77
83
option ,
78
84
customOptions = { } ,
79
85
controlsHidden = true ,
@@ -384,6 +390,7 @@ var halkaBox = (function () {
384
390
385
391
// function for closing popup by clicking on empty space
386
392
function bgClickClose ( event ) {
393
+ if ( isZoomed ) return ;
387
394
// to prevent bubbling
388
395
event . stopPropagation ( ) ;
389
396
event . preventDefault ( ) ;
@@ -412,12 +419,13 @@ var halkaBox = (function () {
412
419
// zoom related functions
413
420
function resetZoom ( img ) {
414
421
img . removeAttribute ( "style" ) ;
415
- img . style . transition = "all 150ms ease-out " ;
422
+ img . style . transition = "all 150ms ease-in " ;
416
423
zoomPercentage = 100 ;
417
- isZoomed = false ;
418
424
window . setTimeout ( function ( ) {
425
+ isZoomed = false ;
426
+ img . style . transition = "" ;
419
427
img . removeAttribute ( "style" ) ;
420
- } , 160 ) ;
428
+ } , 200 ) ;
421
429
}
422
430
function checkCorners ( img ) {
423
431
var imgComputedWidth = parseInt ( window . getComputedStyle ( img , null ) . getPropertyValue ( "width" ) ) ;
@@ -465,16 +473,18 @@ var halkaBox = (function () {
465
473
466
474
if ( imgComputedWidth <= window . innerWidth && imgComputedHeight <= window . innerHeight ) {
467
475
resetZoom ( img ) ;
476
+ return ;
468
477
}
469
478
470
479
window . setTimeout ( function ( ) {
471
480
img . style . transition = "" ;
472
- } , 160 ) ;
481
+ } , 200 ) ;
473
482
}
474
483
function moveImage ( x , y , img ) {
475
484
var numericLeft = window . getComputedStyle ( img , null ) . getPropertyValue ( "left" ) ;
476
485
var numericTop = window . getComputedStyle ( img , null ) . getPropertyValue ( "top" ) ;
477
486
487
+ img . style . transition = "" ;
478
488
img . style . position = "absolute" ;
479
489
480
490
img . style . top = ( parseInt ( numericTop ) + ( y ) ) + "px" ;
@@ -485,6 +495,9 @@ var halkaBox = (function () {
485
495
function zoomImage ( img ) {
486
496
if ( ! customOptions . isZoomable ) return
487
497
498
+ hideCaption ( ) ;
499
+ hideControls ( ) ;
500
+
488
501
var imgComputedWidth = parseInt ( window . getComputedStyle ( img , null ) . getPropertyValue ( "width" ) ) ;
489
502
var imgComputedHeight = parseInt ( window . getComputedStyle ( img , null ) . getPropertyValue ( "height" ) ) ;
490
503
@@ -572,8 +585,38 @@ var halkaBox = (function () {
572
585
}
573
586
}
574
587
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
576
595
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
+ }
577
620
// if orientation has been changed then set orientPortrait to false or vice versa and set viewort equal to new window.innerWidth
578
621
if ( ( window . innerWidth < window . innerHeight ) !== orientPortrait ) {
579
622
orientPortrait = orientPortrait === true ? false : true ;
@@ -585,12 +628,15 @@ var halkaBox = (function () {
585
628
if ( event . touches . length > 1 ) {
586
629
touch2PositionX = event . touches [ 1 ] . clientX ;
587
630
touch2PositionY = event . touches [ 1 ] . clientY ;
588
- } else {
589
- return false ;
590
631
}
591
632
}
592
633
function touchMove ( event ) {
593
634
event . preventDefault ( ) ;
635
+
636
+ if ( doubleTapFlags . isDoubleTapped ) return ;
637
+
638
+ if ( touchEnabled ) resetDoubleTapFlags ( ) ;
639
+
594
640
var touch = event . touches [ 0 ] || event . changedTouches [ 0 ] ,
595
641
touches = event . touches . length ;
596
642
// to check if touchEnabled is false, touches are not two and browser is not zoomed in
@@ -634,13 +680,17 @@ var halkaBox = (function () {
634
680
touchPositionY = event . touches [ 0 ] . clientY ;
635
681
636
682
moveImage ( touchDiffX , touchDiffY , imageObjects [ curIndex ] . getElementsByTagName ( "img" ) [ 0 ] ) ;
637
- } else {
638
- return false ;
639
683
}
640
684
}
641
685
function touchEnd ( ) {
642
686
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 ) ;
644
694
}
645
695
646
696
// functions to hide controls / captions
0 commit comments