diff --git a/README.md b/README.md index 51b9291..5df99ec 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ var lightbox2 = $('.lighbox-2 a').simpleLightbox(); ``` ### Changelog +**1.8.1 - Bugfix for #31, #32 and #33** **1.8.0 - New API Events (changed open to show) and little fix in function open() brought by Geoffrey Crofte and some other small bugfixes** **1.7.2 - Bugfix von #25 and #27** **1.7.1 - Bugfix von #22 with new option alertError and merged pull request #23** diff --git a/bower.json b/bower.json index ad87481..3a31759 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "simplelightbox", - "version": "1.8.0", + "version": "1.8.1", "homepage": "http://andreknieriem.de/simple-lightbox", "authors": [ "André Rinas (http://andreknieriem.de)" diff --git a/dist/simple-lightbox.js b/dist/simple-lightbox.js index c320b5f..c217cc5 100644 --- a/dist/simple-lightbox.js +++ b/dist/simple-lightbox.js @@ -73,8 +73,8 @@ $.fn.simpleLightbox = function( options ) return false; }, opened = false, - - objects = $( this.selector, this.context ), + loaded = [], + objects = this, transPrefix = transPrefix(), canTransisions = (transPrefix !== false) ? true : false, prefix = 'simplelb', @@ -115,6 +115,9 @@ $.fn.simpleLightbox = function( options ) curImg = $( '' ) .hide() .attr('src', elem.attr('href')); + if(loaded.indexOf(elem.attr('href')) == -1){ + loaded.push(elem.attr('href')); + } $('.sl-image').html('').attr('style',''); curImg.appendTo($('.sl-image')); overlay.fadeIn('fast'); @@ -152,7 +155,6 @@ $.fn.simpleLightbox = function( options ) return; } }) - tmpImage.onload = function() { if (typeof dir !== 'undefined') { objects.eq(index) @@ -160,6 +162,9 @@ $.fn.simpleLightbox = function( options ) .trigger($.Event( (dir===1?'nextDone':'prevDone')+'.simplelightbox')); } + if(loaded.indexOf(curImg.attr( 'src' )) == -1){ + loaded.push(curImg.attr( 'src' )); + } var imageWidth = tmpImage.width, imageHeight = tmpImage.height; @@ -236,9 +241,15 @@ $.fn.simpleLightbox = function( options ) var next = (index+1 < 0) ? objects.length -1: (index+1 >= objects.length -1) ? 0 : index+1, prev = (index-1 < 0) ? objects.length -1: (index-1 >= objects.length -1) ? 0 : index-1; $( '' ).attr( 'src', objects.eq(next).attr( 'href' ) ).load(function(){ + if(loaded.indexOf($(this).attr('src')) == -1){ + loaded.push($(this).attr('src')); + } objects.eq(index).trigger($.Event('nextImageLoaded.simplelightbox')); }); $( '' ).attr( 'src', objects.eq(prev).attr( 'href' ) ).load(function(){ + if(loaded.indexOf($(this).attr('src')) == -1){ + loaded.push($(this).attr('src')); + } objects.eq(index).trigger($.Event('prevImageLoaded.simplelightbox')); }); @@ -248,7 +259,6 @@ $.fn.simpleLightbox = function( options ) .trigger($.Event('change.simplelightbox')) .trigger($.Event( (dir===1?'next':'prev')+'.simplelightbox')); - spinner.show(); var newIndex = index + dir; if(animating || (newIndex < 0 || newIndex >= objects.length) && options.loop == false ) return; index = (newIndex < 0) ? objects.length -1: (newIndex > objects.length -1) ? 0 : newIndex; @@ -258,12 +268,16 @@ $.fn.simpleLightbox = function( options ) if( canTransisions ) slide(options.animationSpeed / 1000, ( -100 * dir ) - swipeDiff + 'px'); else css.left = parseInt( $('.sl-image').css( 'left' ) ) + -100 * dir + 'px'; } + $('.sl-image').animate( css, options.animationSpeed, function(){ setTimeout( function(){ // fadeout old image var elem = objects.eq(index); curImg .attr('src', elem.attr('href')); + if(loaded.indexOf(elem.attr('href')) == -1){ + spinner.show(); + } $('.sl-caption').remove(); adjustImage(dir); if(options.preloading) preload(); @@ -305,7 +319,7 @@ $.fn.simpleLightbox = function( options ) } } } else { - $('body').css({'padding-right':$('body').data('padding'), 'overflow':'auto'}); + $('body').css({'padding-right':$('body').data('padding'), 'overflow':'visible'}); } } diff --git a/dist/simple-lightbox.min.js b/dist/simple-lightbox.min.js index 8dc16b7..a4a0962 100644 --- a/dist/simple-lightbox.min.js +++ b/dist/simple-lightbox.min.js @@ -2,4 +2,4 @@ By André Rinas, www.andreknieriem.de Available for use under the MIT License */ -!function(e,t,n,i){"use strict";e.fn.simpleLightbox=function(i){var i=e.extend({overlay:!0,spinner:!0,nav:!0,navText:["‹","›"],captions:!0,captionDelay:0,captionSelector:"img",captionType:"attr",captionsData:"title",captionPosition:"bottom",close:!0,closeText:"×",showCounter:!0,fileExt:"png|jpg|jpeg|gif",animationSlide:!0,animationSpeed:250,preloading:!0,enableKeyboard:!0,loop:!0,docClose:!0,swipeTolerance:50,className:"simple-lightbox",widthRatio:.8,heightRatio:.9,disableRightClick:!1,disableScroll:!0,alertError:!0,alertErrorMessage:"Image not found, next image will be loaded"},i),a=(t.navigator.pointerEnabled||t.navigator.msPointerEnabled,0),o=e(),l=function(){var e=n.body||n.documentElement,e=e.style;return""==e.WebkitTransition?"-webkit-":""==e.MozTransition?"-moz-":""==e.OTransition?"-o-":""==e.transition?"":!1},s=!1,r=e(this.selector,this.context),l=l(),d=l!==!1?!0:!1,p="simplelb",c=e("
").addClass("sl-overlay"),g=e(""),u=e("
").addClass("sl-counter").html('/'),m=!1,v=0,x=e(),b=e("
").addClass("sl-caption pos-"+i.captionPosition),y=e("
").addClass("sl-wrapper").addClass(i.className).html('
'),w=function(t){return i.fileExt?"a"==e(t).prop("tagName").toLowerCase()&&new RegExp(".("+i.fileExt+")$","i").test(e(t).attr("href")):!0},E=function(){x=e(".sl-image"),i.close&&g.appendTo(y),i.showCounter&&r.length>1&&(u.appendTo(y),u.find(".sl-total").text(r.length)),i.nav&&f.appendTo(y),i.spinner&&h.appendTo(y)},T=function(t){t.trigger(e.Event("show.simplelightbox")),i.disableScroll&&M("hide"),y.appendTo("body"),i.overlay&&c.appendTo(e("body")),m=!0,v=r.index(t),o=e("").hide().attr("src",t.attr("href")),e(".sl-image").html("").attr("style",""),o.appendTo(e(".sl-image")),c.fadeIn("fast"),e(".sl-close").fadeIn("fast"),h.show(),f.fadeIn("fast"),e(".sl-wrapper .sl-counter .sl-current").text(v+1),u.fadeIn("fast"),S(),i.preloading&&I(),setTimeout(function(){t.trigger(e.Event("shown.simplelightbox"))},i.animationSpeed)},S=function(n){if(o.length){var a=new Image,l=e(t).width()*i.widthRatio,p=e(t).height()*i.heightRatio;a.src=o.attr("src"),e(a).bind("error",function(t){return r.eq(v).trigger(e.Event("error.simplelightbox")),m=!1,s=!0,h.hide(),i.alertError?(alert(i.alertErrorMessage),void q(1==n||-1==n?n:1)):void 0}),a.onload=function(){"undefined"!=typeof n&&r.eq(v).trigger(e.Event("changed.simplelightbox")).trigger(e.Event((1===n?"nextDone":"prevDone")+".simplelightbox"));var c=a.width,g=a.height;if(c>l||g>p){var f=c/g>l/p?c/l:g/p;c/=f,g/=f}e(".sl-image").css({top:(e(t).height()-g)/2+"px",left:(e(t).width()-c)/2+"px"}),h.hide(),o.css({width:c+"px",height:g+"px"}).fadeIn("fast"),s=!0;var u="self"==i.captionSelector?r.eq(v):r.eq(v).find(i.captionSelector);if("data"==i.captionType)var x=u.data(i.captionsData);else if("text"==i.captionType)var x=u.html();else var x=u.prop(i.captionsData);if(i.loop||(0==v&&e(".sl-prev").hide(),v>=r.length-1&&e(".sl-next").hide(),v>0&&vv+1?r.length-1:v+1>=r.length-1?0:v+1,n=0>v-1?r.length-1:v-1>=r.length-1?0:v-1;e("").attr("src",r.eq(t).attr("href")).load(function(){r.eq(v).trigger(e.Event("nextImageLoaded.simplelightbox"))}),e("").attr("src",r.eq(n).attr("href")).load(function(){r.eq(v).trigger(e.Event("prevImageLoaded.simplelightbox"))})},q=function(t){r.eq(v).trigger(e.Event("change.simplelightbox")).trigger(e.Event((1===t?"next":"prev")+".simplelightbox")),h.show();var n=v+t;if(!(m||(0>n||n>=r.length)&&0==i.loop)){v=0>n?r.length-1:n>r.length-1?0:n,e(".sl-wrapper .sl-counter .sl-current").text(v+1);var l={opacity:0};i.animationSlide&&(d?k(i.animationSpeed/1e3,-100*t-a+"px"):l.left=parseInt(e(".sl-image").css("left"))+-100*t+"px"),e(".sl-image").animate(l,i.animationSpeed,function(){setTimeout(function(){var n=r.eq(v);o.attr("src",n.attr("href")),e(".sl-caption").remove(),S(t),i.preloading&&I()},100)})}},D=function(){if(!m){var t=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(){i.disableScroll&&M("show"),e(".sl-wrapper, .sl-overlay").remove(),n||t.trigger(e.Event("closed.simplelightbox")),n=!0}),o=e(),s=!1,m=!1}},M=function(i){if("hide"==i){var a=t.innerWidth;if(!a){var o=n.documentElement.getBoundingClientRect();a=o.right-Math.abs(o.left)}if(n.body.clientWidth0&&e("body").css({"padding-right":s+r,overflow:"hidden"})}}else e("body").css({"padding-right":e("body").data("padding"),overflow:"auto"})};E(),e(t).on("resize",S),r.on("click."+p,function(t){if(w(this)){if(t.preventDefault(),m)return!1;T(e(this))}}),e(n).on("click",".sl-close",function(e){e.preventDefault(),s&&D()}),e(n).click(function(t){s&&i.docClose&&0==e(t.target).closest(".sl-image").length&&0==e(t.target).closest(".sl-navigation").length&&D()}),i.disableRightClick&&e(n).on("contextmenu",".sl-image img",function(e){return!1}),e(n).on("click",".sl-navigation button",function(t){t.preventDefault(),a=0,q(e(this).hasClass("sl-next")?1:-1)}),i.enableKeyboard&&e(n).on("keyup."+p,function(e){if(e.preventDefault(),a=0,s){var t=e.keyCode;27==t&&D(),(37==t||39==e.keyCode)&&q(39==e.keyCode?1:-1)}});var R=0,P=0,W=!1,X=0;return e(n).on("touchstart mousedown pointerdown MSPointerDown",".sl-image",function(e){return W?!0:(d&&(X=parseInt(x.css("left"))),W=!0,R=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,!1)}).on("touchmove mousemove pointermove MSPointerMove",function(e){return W?(e.preventDefault(),P=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,a=R-P,void(i.animationSlide&&(d?k(0,-a+"px"):x.css("left",X-a+"px")))):!0}).on("touchend mouseup touchcancel pointerup pointercancel MSPointerUp MSPointerCancel",function(e){W&&(W=!1,Math.abs(a)>i.swipeTolerance?q(a>0?1:-1):i.animationSlide&&(d?k(i.animationSpeed/1e3,"0px"):x.animate({left:X+"px"},i.animationSpeed/2)))}),this.open=function(t){t=t||e(this[0]),T(t)},this.next=function(){q(1)},this.prev=function(){q(-1)},this.close=function(){D()},this.destroy=function(){e(n).unbind("click."+p).unbind("keyup."+p),D(),e(".sl-overlay, .sl-wrapper").remove()},this}}(jQuery,window,document); \ No newline at end of file +!function(e,t,n,i){"use strict";e.fn.simpleLightbox=function(i){var i=e.extend({overlay:!0,spinner:!0,nav:!0,navText:["‹","›"],captions:!0,captionDelay:0,captionSelector:"img",captionType:"attr",captionsData:"title",captionPosition:"bottom",close:!0,closeText:"×",showCounter:!0,fileExt:"png|jpg|jpeg|gif",animationSlide:!0,animationSpeed:250,preloading:!0,enableKeyboard:!0,loop:!0,docClose:!0,swipeTolerance:50,className:"simple-lightbox",widthRatio:.8,heightRatio:.9,disableRightClick:!1,disableScroll:!0,alertError:!0,alertErrorMessage:"Image not found, next image will be loaded"},i),a=(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=[],d=this,s=s(),p=s!==!1?!0:!1,c="simplelb",g=e("
").addClass("sl-overlay"),h=e(""),m=e("
").addClass("sl-counter").html('/'),v=!1,x=0,b=e(),y=e("
").addClass("sl-caption pos-"+i.captionPosition),w=e("
").addClass("sl-wrapper").addClass(i.className).html('
'),E=function(t){return i.fileExt?"a"==e(t).prop("tagName").toLowerCase()&&new RegExp(".("+i.fileExt+")$","i").test(e(t).attr("href")):!0},T=function(){b=e(".sl-image"),i.close&&h.appendTo(w),i.showCounter&&d.length>1&&(m.appendTo(w),m.find(".sl-total").text(d.length)),i.nav&&u.appendTo(w),i.spinner&&f.appendTo(w)},S=function(t){t.trigger(e.Event("show.simplelightbox")),i.disableScroll&&R("hide"),w.appendTo("body"),i.overlay&&g.appendTo(e("body")),v=!0,x=d.index(t),o=e("").hide().attr("src",t.attr("href")),-1==r.indexOf(t.attr("href"))&&r.push(t.attr("href")),e(".sl-image").html("").attr("style",""),o.appendTo(e(".sl-image")),g.fadeIn("fast"),e(".sl-close").fadeIn("fast"),f.show(),u.fadeIn("fast"),e(".sl-wrapper .sl-counter .sl-current").text(x+1),m.fadeIn("fast"),C(),i.preloading&&q(),setTimeout(function(){t.trigger(e.Event("shown.simplelightbox"))},i.animationSpeed)},C=function(n){if(o.length){var a=new Image,s=e(t).width()*i.widthRatio,c=e(t).height()*i.heightRatio;a.src=o.attr("src"),e(a).bind("error",function(t){return d.eq(x).trigger(e.Event("error.simplelightbox")),v=!1,l=!0,f.hide(),i.alertError?(alert(i.alertErrorMessage),void D(1==n||-1==n?n:1)):void 0}),a.onload=function(){"undefined"!=typeof n&&d.eq(x).trigger(e.Event("changed.simplelightbox")).trigger(e.Event((1===n?"nextDone":"prevDone")+".simplelightbox")),-1==r.indexOf(o.attr("src"))&&r.push(o.attr("src"));var g=a.width,h=a.height;if(g>s||h>c){var u=g/h>s/c?g/s:h/c;g/=u,h/=u}e(".sl-image").css({top:(e(t).height()-h)/2+"px",left:(e(t).width()-g)/2+"px"}),f.hide(),o.css({width:g+"px",height:h+"px"}).fadeIn("fast"),l=!0;var m="self"==i.captionSelector?d.eq(x):d.eq(x).find(i.captionSelector);if("data"==i.captionType)var b=m.data(i.captionsData);else if("text"==i.captionType)var b=m.html();else var b=m.prop(i.captionsData);if(i.loop||(0==x&&e(".sl-prev").hide(),x>=d.length-1&&e(".sl-next").hide(),x>0&&xx+1?d.length-1:x+1>=d.length-1?0:x+1,n=0>x-1?d.length-1:x-1>=d.length-1?0:x-1;e("").attr("src",d.eq(t).attr("href")).load(function(){-1==r.indexOf(e(this).attr("src"))&&r.push(e(this).attr("src")),d.eq(x).trigger(e.Event("nextImageLoaded.simplelightbox"))}),e("").attr("src",d.eq(n).attr("href")).load(function(){-1==r.indexOf(e(this).attr("src"))&&r.push(e(this).attr("src")),d.eq(x).trigger(e.Event("prevImageLoaded.simplelightbox"))})},D=function(t){d.eq(x).trigger(e.Event("change.simplelightbox")).trigger(e.Event((1===t?"next":"prev")+".simplelightbox"));var n=x+t;if(!(v||(0>n||n>=d.length)&&0==i.loop)){x=0>n?d.length-1:n>d.length-1?0:n,e(".sl-wrapper .sl-counter .sl-current").text(x+1);var s={opacity:0};i.animationSlide&&(p?I(i.animationSpeed/1e3,-100*t-a+"px"):s.left=parseInt(e(".sl-image").css("left"))+-100*t+"px"),e(".sl-image").animate(s,i.animationSpeed,function(){setTimeout(function(){var n=d.eq(x);o.attr("src",n.attr("href")),-1==r.indexOf(n.attr("href"))&&f.show(),e(".sl-caption").remove(),C(t),i.preloading&&q()},100)})}},M=function(){if(!v){var t=d.eq(x),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(){i.disableScroll&&R("show"),e(".sl-wrapper, .sl-overlay").remove(),n||t.trigger(e.Event("closed.simplelightbox")),n=!0}),o=e(),l=!1,v=!1}},R=function(i){if("hide"==i){var a=t.innerWidth;if(!a){var o=n.documentElement.getBoundingClientRect();a=o.right-Math.abs(o.left)}if(n.body.clientWidth0&&e("body").css({"padding-right":l+r,overflow:"hidden"})}}else e("body").css({"padding-right":e("body").data("padding"),overflow:"visible"})};T(),e(t).on("resize",C),d.on("click."+c,function(t){if(E(this)){if(t.preventDefault(),v)return!1;S(e(this))}}),e(n).on("click",".sl-close",function(e){e.preventDefault(),l&&M()}),e(n).click(function(t){l&&i.docClose&&0==e(t.target).closest(".sl-image").length&&0==e(t.target).closest(".sl-navigation").length&&M()}),i.disableRightClick&&e(n).on("contextmenu",".sl-image img",function(e){return!1}),e(n).on("click",".sl-navigation button",function(t){t.preventDefault(),a=0,D(e(this).hasClass("sl-next")?1:-1)}),i.enableKeyboard&&e(n).on("keyup."+c,function(e){if(e.preventDefault(),a=0,l){var t=e.keyCode;27==t&&M(),(37==t||39==e.keyCode)&&D(39==e.keyCode?1:-1)}});var O=0,P=0,W=!1,X=0;return e(n).on("touchstart mousedown pointerdown MSPointerDown",".sl-image",function(e){return W?!0:(p&&(X=parseInt(b.css("left"))),W=!0,O=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,!1)}).on("touchmove mousemove pointermove MSPointerMove",function(e){return W?(e.preventDefault(),P=e.originalEvent.pageX||e.originalEvent.touches[0].pageX,a=O-P,void(i.animationSlide&&(p?I(0,-a+"px"):b.css("left",X-a+"px")))):!0}).on("touchend mouseup touchcancel pointerup pointercancel MSPointerUp MSPointerCancel",function(e){W&&(W=!1,Math.abs(a)>i.swipeTolerance?D(a>0?1:-1):i.animationSlide&&(p?I(i.animationSpeed/1e3,"0px"):b.animate({left:X+"px"},i.animationSpeed/2)))}),this.open=function(t){t=t||e(this[0]),S(t)},this.next=function(){D(1)},this.prev=function(){D(-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 diff --git a/package.json b/package.json index 2d8092f..ef22ec3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simplelightbox", - "version": "1.8.0", + "version": "1.8.1", "description": "Touch-friendly image lightbox for mobile and desktop with jQuery", "main": "dist/simple-lightbox.js", "repository": {