Responsive and touch‑friendly image lightbox

http://osvaldas.info/image-lightbox-responsive-touch-friendly

by bvotcode

HTML

<script src="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/imagelightbox.min.js"></script>
<a href="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/full/1.jpg" data-imagelightbox="f">
    <img src="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/thumb/1.jpg" alt="1">
</a>
<a href="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/full/2.jpg" data-imagelightbox="f">
    <img src="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/thumb/2.jpg" alt="2b">
</a>
<a href="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/full/3.jpg" data-imagelightbox="f">
    <img src="http://osvaldas.info/examples/image-lightbox-responsive-touch-friendly/thumb/3.jpg" alt="3a">
</a>

CSS

body {
    padding: 1em;
}
/* IMAGE LIGHTBOX SELECTOR */

#imagelightbox
{
    cursor: pointer;
    position: fixed;
    z-index: 10000;

    -ms-touch-action: none;
    touch-action: none;

    -webkit-box-shadow: 0 0 3.125em rgba( 0, 0, 0, .75 ); /* 50 */
    box-shadow: 0 0 3.125em rgba( 0, 0, 0, .75 ); /* 50 */
}


/* ACTIVITY INDICATION */

#imagelightbox-loading,
#imagelightbox-loading div
{
    border-radius: 50%;
}
#imagelightbox-loading
{
    width: 2.5em; /* 40 */
    height: 2.5em; /* 40 */
    background-color: #444;
    background-color: rgba( 0, 0, 0, .5 );
    position: fixed;
    z-index: 10003;
    top: 50%;
    left: 50%;
    padding: 0.625em; /* 10 */
    margin: -1.25em 0 0 -1.25em; /* 20 */

    -webkit-box-shadow: 0 0 2.5em rgba( 0, 0, 0, .75 ); /* 40 */
    box-shadow: 0 0 2.5em rgba( 0, 0, 0, .75 ); /* 40 */
}
#imagelightbox-loading div
{
    width: 1.25em; /* 20 */
    height: 1.25em; /* 20 */
    background-color: #fff;

    -webkit-animation: imagelightbox-loading .5s ease infinite;
    animation: imagelightbox-loading .5s ease infinite;
}

@-webkit-keyframes imagelightbox-loading
{
    from { opacity: .5;	-webkit-transform: scale( .75 ); }
    50%	 { opacity: 1;	-webkit-transform: scale( 1 ); }
    to	 { opacity: .5;	-webkit-transform: scale( .75 ); }
}
@keyframes imagelightbox-loading
{
    from { opacity: .5;	transform: scale( .75 ); }
    50%	 { opacity: 1;	transform: scale( 1 ); }
    to	 { opacity: .5;	transform: scale( .75 ); }
}


/* OVERLAY */

#imagelightbox-overlay
{
    background-color: #fff;
    background-color: rgba( 255, 255, 255, .9 );
    position: fixed;
    z-index: 9998;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}


/* "CLOSE" BUTTON */

#imagelightbox-close
{
    width: 2.5em; /* 40 */
    height: 2.5em; /* 40 */
    text-align: left;
    background-color: #666;
    border-radius: 50%;
    position: fixed;
    z-index: 10002;
    top: 2.5em; /* 40 */
    right: 2.5em; /* 40 */

    -webkit-transition:...

JavaScript

// ACTIVITY INDICATOR

		var activityIndicatorOn = function()
			{
				$( '<div id="imagelightbox-loading"><div></div></div>' ).appendTo( 'body' );
			},
			activityIndicatorOff = function()
			{
				$( '#imagelightbox-loading' ).remove();
			},


			// OVERLAY

			overlayOn = function()
			{
				$( '<div id="imagelightbox-overlay"></div>' ).appendTo( 'body' );
			},
			overlayOff = function()
			{
				$( '#imagelightbox-overlay' ).remove();
			},


			// CLOSE BUTTON

			closeButtonOn = function( instance )
			{
				$( '<button type="button" id="imagelightbox-close" title="Close"></button>' ).appendTo( 'body' ).on( 'click touchend', function(){ $( this ).remove(); instance.quitImageLightbox(); return false; });
			},
			closeButtonOff = function()
			{
				$( '#imagelightbox-close' ).remove();
			},


			// CAPTION

			captionOn = function()
			{
				var description = $( 'a[href="' + $( '#imagelightbox' ).attr( 'src' ) + '"] img' ).attr( 'alt' );
				if( description.length > 0 )
					$( '<div id="imagelightbox-caption">' + description + '</div>' ).appendTo( 'body' );
			},
			captionOff = function()
			{
				$( '#imagelightbox-caption' ).remove();
			},


			// NAVIGATION

			navigationOn = function( instance, selector )
			{
				var images = $( selector );
				if( images.length )
				{
					var nav = $( '<div id="imagelightbox-nav"></div>' );
					for( var i = 0; i < images.length; i++ )
						nav.append( '<button type="button"></button>' );

					nav.appendTo( 'body' );
					nav.on( 'click touchend', function(){ return false; });

					var navItems = nav.find( 'button' );
					navItems.on( 'click touchend', function()
					{
						var $this = $( this );
						if( images.eq( $this.index() ).attr( 'href' ) != $( '#imagelightbox' ).attr( 'src' ) )
							instance.switchImageLightbox( $this.index() );

						navItems.removeClass( 'active' );
						navItems.eq( $this.index() ).addClass( 'active' );

						return false;
					})
					.on( 'touchend', function(){...