From 5d034549cb5a619fb1c60ffd38b1f2a04db5de4e Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Sep 2015 19:08:44 +0200 Subject: [PATCH] disable keyboard control if lightbox is closed --- README.md | 1 + bower.json | 2 +- dist/simple-lightbox.js | 15 +++++++++------ dist/simple-lightbox.min.js | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 23c2823..5581544 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ var gallery = $('.gallery a').simpleLightbox(); gallery.next(); // Next Image ``` ### Changelog +**1.3.1 - Bugfix: disable keyboard control if lightbox is closed** **1.3.0 - Added current index indicator/counter** **1.2.0 - Added option for captions attribute (title or data-title)** **1.1.2 - Bugfix for looping images** diff --git a/bower.json b/bower.json index d52ea42..3bad7f1 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "simplelightbox", - "version": "1.3.0", + "version": "1.3.1", "homepage": "http://andreknieriem.de/simple-lightbox", "authors": [ "AndrĂ© Knieriem (http://andreknieriem.de)" diff --git a/dist/simple-lightbox.js b/dist/simple-lightbox.js index 773fab5..75f46fc 100644 --- a/dist/simple-lightbox.js +++ b/dist/simple-lightbox.js @@ -264,12 +264,15 @@ $.fn.simpleLightbox = function( options ) $( document ).on( 'keyup.'+prefix, function( e ){ e.preventDefault(); swipeDiff = 0; - var key = e.keyCode; - if( key == 27 ) { - close(); - } - if( key == 37 || e.keyCode == 39 ) { - loadImage( e.keyCode == 39 ? 1 : -1 ); + // keyboard control only if lightbox is open + if(opened){ + var key = e.keyCode; + if( key == 27 ) { + close(); + } + if( key == 37 || e.keyCode == 39 ) { + loadImage( e.keyCode == 39 ? 1 : -1 ); + } } }); } diff --git a/dist/simple-lightbox.min.js b/dist/simple-lightbox.min.js index 9f68df2..656ad43 100644 --- a/dist/simple-lightbox.min.js +++ b/dist/simple-lightbox.min.js @@ -2,4 +2,4 @@ By Andre Knieriem, www.andreknieriem.de Available for use under the MIT License */ -!function(e,t,n){"use strict";e.fn.simpleLightbox=function(a){var a=e.extend({overlay:!0,spinner:!0,nav:!0,navText:["←","→"],captions:!0,close:!0,closeText:"X",showCounter:!0,fileExt:"png|jpg|jpeg|gif",animationSpeed:250,preloading:!0,enableKeyboard:!0,loop:!0,docClose:!0,swipeTolerance:50,className:"simple-lightbox",widthRatio:.8,heightRatio:.9},a),i=(t.navigator.pointerEnabled||t.navigator.msPointerEnabled,0),o=e(),s=function(){var e=n.body||n.documentElement,e=e.style;return""==e.WebkitTransition?"-webkit-":""==e.MozTransition?"-moz-":""==e.OTransition?"-o-":""==e.transition?"":!1},l=!1,r=this.selector,s=s(),p=s!==!1?!0:!1,c="simplelb",d=e("
").addClass("sl-overlay"),u=e(""),h=e("
").addClass("sl-counter").html('/'),m=!1,v=0,x=e(),w=e("
").addClass("sl-caption"),b=function(t){return"a"==e(t).prop("tagName").toLowerCase()&&new RegExp(".("+a.fileExt+")$","i").test(e(t).attr("href"))},T=function(){a.overlay&&d.appendTo(e("body")),e("
").addClass("sl-wrapper").addClass(a.className).html('
').appendTo("body"),x=e(".sl-image"),a.close&&u.appendTo(e(".sl-wrapper")),a.showCounter&&e(r).length>1&&(h.appendTo(e(".sl-wrapper")),e(".sl-wrapper .sl-counter .sl-total").text(e(r).length)),a.nav&&f.appendTo(e(".sl-wrapper")),a.spinner&&g.appendTo(e(".sl-wrapper"))},y=function(t){t.trigger(e.Event("show.simplelightbox")),m=!0,v=e(r).index(t),o=e("").hide().attr("src",t.attr("href")),e(".sl-image").html(""),o.appendTo(e(".sl-image")),d.fadeIn("fast"),e(".sl-close").fadeIn("fast"),g.show(),f.fadeIn("fast"),e(".sl-wrapper .sl-counter .sl-current").text(v+1),h.fadeIn("fast"),C(),a.preloading&&S(),setTimeout(function(){t.trigger(e.Event("shown.simplelightbox"))},a.animationSpeed)},C=function(n){if(o.length){var i=new Image,s=e(t).width()*a.widthRatio,c=e(t).height()*a.heightRatio;i.src=o.attr("src"),i.onload=function(){var d=i.width,u=i.height;if(d>s||u>c){var f=d/u>s/c?d/s:u/c;d/=f,u/=f}e(".sl-image").css({top:(e(t).height()-u)/2+"px",left:(e(t).width()-d)/2+"px"}),g.hide(),o.css({width:d+"px",height:u+"px"}).fadeIn("fast"),l=!0;var h=e(r).eq(v).find("img").prop("title");if(1==n||-1==n){var x={opacity:1};p?(k(0,100*n+"px"),setTimeout(function(){k(a.animationSpeed/1e3,"0px"),50})):x.left=parseInt(e(".sl-image").css("left"))+100*n+"px",e(".sl-image").animate(x,a.animationSpeed,function(){m=!1,E(h)})}else m=!1,E(h)}}},E=function(t){""!=t&&a.captions&&w.text(t).hide().appendTo(e(".sl-image")).fadeIn("fast")},k=function(t,n){var a={};a[s+"transform"]="translateX("+n+")",a[s+"transition"]=s+"transform "+t+"s linear",e(".sl-image").css(a)},S=function(){var t=0>v+1?e(r).length-1:v+1>=e(r).length-1?0:v+1,n=0>v-1?e(r).length-1:v-1>=e(r).length-1?0:v-1;e("").attr("src",e(r).eq(t).attr("href")).load(),e("").attr("src",e(r).eq(n).attr("href")).load()},I=function(t){g.show();var n=v+t;if(!(m||(0>n||n>=e(r).length)&&0==a.loop)){m=!0,v=0>n?e(r).length-1:n>e(r).length-1?0:n,e(".sl-wrapper .sl-counter .sl-current").text(v+1);var s={opacity:0};p?k(a.animationSpeed/1e3,-100*t-i+"px"):s.left=parseInt(e(".sl-image").css("left"))+-100*t+"px",e(".sl-image").animate(s,a.animationSpeed,function(){setTimeout(function(){var n=e(r).eq(v);o.attr("src",n.attr("href")),e(".sl-caption").remove(),C(t),a.preloading&&S()},100)})}},M=function(){var t=e(r).eq(v),n=!1;t.trigger(e.Event("close.simplelightbox")),e(".sl-image img, .sl-overlay, .sl-close, .sl-navigation, .sl-image .sl-caption, .sl-counter").fadeOut("fast",function(){n||t.trigger(e.Event("closed.simplelightbox")),n=!0}),o=e(),l=!1};T(),e(t).on("resize",C),e(n).on("click."+c,this.selector,function(t){if(b(this)){if(t.preventDefault(),m)return!1;y(e(this))}}),e(n).on("click",".sl-close",function(e){e.preventDefault(),l&&M()}),e(n).click(function(t){l&&a.docClose&&0==e(t.target).closest(".sl-image").length&&0==e(t.target).closest(".sl-navigation").length&&M()}),e(n).on("click",".sl-navigation button",function(t){t.preventDefault(),i=0,I(e(this).hasClass("sl-next")?1:-1)}),a.enableKeyboard&&e(n).on("keyup."+c,function(e){e.preventDefault(),i=0;var t=e.keyCode;27==t&&M(),(37==t||39==e.keyCode)&&I(39==e.keyCode?1:-1)});var D=0,X=0,q=!1,P=0;return e(n).on("touchstart mousedown pointerdown MSPointerDown",".sl-image",function(e){return q?!0:(p&&(P=parseInt(x.css("left"))),q=!0,void(D=e.originalEvent.pageX||e.originalEvent.touches[0].pageX))}).on("touchmove mousemove pointermove MSPointerMove",".sl-image",function(e){return q?(e.preventDefault(),X=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,i=D-X,void(p?k(0,-i+"px"):x.css("left",P-i+"px"))):!0}).on("touchend mouseup touchcancel pointerup pointercancel MSPointerUp MSPointerCancel",".sl-image",function(){q=!1,Math.abs(i)>a.swipeTolerance?I(i>0?1:-1):p?k(a.animationSpeed/1e3,"0px"):x.animate({left:P+"px"},a.animationSpeed/2)}),this.open=function(e){y(e)},this.next=function(){I(1)},this.prev=function(){I(-1)},this.close=function(){M()},this.destroy=function(){e(n).unbind("click."+c).unbind("keyup."+c),M(),e(".sl-overlay, .sl-wrapper").remove()},this}}(jQuery,window,document); \ No newline at end of file +!function(e,t,n){"use strict";e.fn.simpleLightbox=function(a){var a=e.extend({overlay:!0,spinner:!0,nav:!0,navText:["←","→"],captions:!0,close:!0,closeText:"X",showCounter:!0,fileExt:"png|jpg|jpeg|gif",animationSpeed:250,preloading:!0,enableKeyboard:!0,loop:!0,docClose:!0,swipeTolerance:50,className:"simple-lightbox",widthRatio:.8,heightRatio:.9},a),i=(t.navigator.pointerEnabled||t.navigator.msPointerEnabled,0),o=e(),s=function(){var e=n.body||n.documentElement,e=e.style;return""==e.WebkitTransition?"-webkit-":""==e.MozTransition?"-moz-":""==e.OTransition?"-o-":""==e.transition?"":!1},l=!1,r=this.selector,s=s(),p=s!==!1?!0:!1,c="simplelb",d=e("
").addClass("sl-overlay"),u=e(""),h=e("
").addClass("sl-counter").html('/'),m=!1,v=0,x=e(),w=e("
").addClass("sl-caption"),b=function(t){return"a"==e(t).prop("tagName").toLowerCase()&&new RegExp(".("+a.fileExt+")$","i").test(e(t).attr("href"))},T=function(){a.overlay&&d.appendTo(e("body")),e("
").addClass("sl-wrapper").addClass(a.className).html('
').appendTo("body"),x=e(".sl-image"),a.close&&u.appendTo(e(".sl-wrapper")),a.showCounter&&e(r).length>1&&(h.appendTo(e(".sl-wrapper")),e(".sl-wrapper .sl-counter .sl-total").text(e(r).length)),a.nav&&f.appendTo(e(".sl-wrapper")),a.spinner&&g.appendTo(e(".sl-wrapper"))},y=function(t){t.trigger(e.Event("show.simplelightbox")),m=!0,v=e(r).index(t),o=e("").hide().attr("src",t.attr("href")),e(".sl-image").html(""),o.appendTo(e(".sl-image")),d.fadeIn("fast"),e(".sl-close").fadeIn("fast"),g.show(),f.fadeIn("fast"),e(".sl-wrapper .sl-counter .sl-current").text(v+1),h.fadeIn("fast"),C(),a.preloading&&S(),setTimeout(function(){t.trigger(e.Event("shown.simplelightbox"))},a.animationSpeed)},C=function(n){if(o.length){var i=new Image,s=e(t).width()*a.widthRatio,c=e(t).height()*a.heightRatio;i.src=o.attr("src"),i.onload=function(){var d=i.width,u=i.height;if(d>s||u>c){var f=d/u>s/c?d/s:u/c;d/=f,u/=f}e(".sl-image").css({top:(e(t).height()-u)/2+"px",left:(e(t).width()-d)/2+"px"}),g.hide(),o.css({width:d+"px",height:u+"px"}).fadeIn("fast"),l=!0;var h=e(r).eq(v).find("img").prop("title");if(1==n||-1==n){var x={opacity:1};p?(k(0,100*n+"px"),setTimeout(function(){k(a.animationSpeed/1e3,"0px"),50})):x.left=parseInt(e(".sl-image").css("left"))+100*n+"px",e(".sl-image").animate(x,a.animationSpeed,function(){m=!1,E(h)})}else m=!1,E(h)}}},E=function(t){""!=t&&a.captions&&w.text(t).hide().appendTo(e(".sl-image")).fadeIn("fast")},k=function(t,n){var a={};a[s+"transform"]="translateX("+n+")",a[s+"transition"]=s+"transform "+t+"s linear",e(".sl-image").css(a)},S=function(){var t=0>v+1?e(r).length-1:v+1>=e(r).length-1?0:v+1,n=0>v-1?e(r).length-1:v-1>=e(r).length-1?0:v-1;e("").attr("src",e(r).eq(t).attr("href")).load(),e("").attr("src",e(r).eq(n).attr("href")).load()},I=function(t){g.show();var n=v+t;if(!(m||(0>n||n>=e(r).length)&&0==a.loop)){m=!0,v=0>n?e(r).length-1:n>e(r).length-1?0:n,e(".sl-wrapper .sl-counter .sl-current").text(v+1);var s={opacity:0};p?k(a.animationSpeed/1e3,-100*t-i+"px"):s.left=parseInt(e(".sl-image").css("left"))+-100*t+"px",e(".sl-image").animate(s,a.animationSpeed,function(){setTimeout(function(){var n=e(r).eq(v);o.attr("src",n.attr("href")),e(".sl-caption").remove(),C(t),a.preloading&&S()},100)})}},M=function(){var t=e(r).eq(v),n=!1;t.trigger(e.Event("close.simplelightbox")),e(".sl-image img, .sl-overlay, .sl-close, .sl-navigation, .sl-image .sl-caption, .sl-counter").fadeOut("fast",function(){n||t.trigger(e.Event("closed.simplelightbox")),n=!0}),o=e(),l=!1};T(),e(t).on("resize",C),e(n).on("click."+c,this.selector,function(t){if(b(this)){if(t.preventDefault(),m)return!1;y(e(this))}}),e(n).on("click",".sl-close",function(e){e.preventDefault(),l&&M()}),e(n).click(function(t){l&&a.docClose&&0==e(t.target).closest(".sl-image").length&&0==e(t.target).closest(".sl-navigation").length&&M()}),e(n).on("click",".sl-navigation button",function(t){t.preventDefault(),i=0,I(e(this).hasClass("sl-next")?1:-1)}),a.enableKeyboard&&e(n).on("keyup."+c,function(e){if(e.preventDefault(),i=0,l){var t=e.keyCode;27==t&&M(),(37==t||39==e.keyCode)&&I(39==e.keyCode?1:-1)}});var D=0,X=0,q=!1,P=0;return e(n).on("touchstart mousedown pointerdown MSPointerDown",".sl-image",function(e){return q?!0:(p&&(P=parseInt(x.css("left"))),q=!0,void(D=e.originalEvent.pageX||e.originalEvent.touches[0].pageX))}).on("touchmove mousemove pointermove MSPointerMove",".sl-image",function(e){return q?(e.preventDefault(),X=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,i=D-X,void(p?k(0,-i+"px"):x.css("left",P-i+"px"))):!0}).on("touchend mouseup touchcancel pointerup pointercancel MSPointerUp MSPointerCancel",".sl-image",function(){q=!1,Math.abs(i)>a.swipeTolerance?I(i>0?1:-1):p?k(a.animationSpeed/1e3,"0px"):x.animate({left:P+"px"},a.animationSpeed/2)}),this.open=function(e){y(e)},this.next=function(){I(1)},this.prev=function(){I(-1)},this.close=function(){M()},this.destroy=function(){e(n).unbind("click."+c).unbind("keyup."+c),M(),e(".sl-overlay, .sl-wrapper").remove()},this}}(jQuery,window,document); \ No newline at end of file