Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit a47d305

Browse files
committed
Reuse slides to improve performance
+ number of small improvements
1 parent 48920a3 commit a47d305

File tree

4 files changed

+130
-56
lines changed

4 files changed

+130
-56
lines changed

dist/jquery.fancybox.js

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ==================================================
2-
// fancyBox v3.0.29
2+
// fancyBox v3.0.30
33
//
44
// Licensed GPLv3 for open source use
55
// or fancyBox Commercial License for commercial use
@@ -231,7 +231,7 @@
231231
}
232232

233233
// Save last active element and current scroll position
234-
self.$lastFocus = $(document.activeElement);
234+
self.$lastFocus = $(document.activeElement).blur();
235235

236236
// Collection of interface DOM elements
237237
self.elems = {};
@@ -526,7 +526,7 @@
526526
$( window ).on('orientationchange.fb resize.fb', function(e) {
527527
requestAFrame(function() {
528528

529-
if ( e.type == "orientationchange" ) {
529+
if ( e && e.originalEvent && e.originalEvent.type == "orientationchange" ) {
530530
self.$refs.slider_wrap.hide();
531531

532532
requestAFrame( runUpdate );
@@ -733,7 +733,7 @@
733733

734734
// Create slides
735735

736-
self.createSlide( pos );
736+
self.current = self.createSlide( pos );
737737

738738
if ( self.group.length > 1 ) {
739739

@@ -746,8 +746,6 @@
746746
}
747747
}
748748

749-
self.current = self.slides[ pos ];
750-
751749
self.current.isMoved = false;
752750
self.current.isComplete = false;
753751

@@ -794,11 +792,31 @@
794792
var self = this;
795793
var $slide;
796794
var index;
795+
var found;
797796

798797
index = pos % self.group.length;
799798
index = index < 0 ? self.group.length + index : index;
800799

801800
if ( !self.slides[ pos ] && self.group[ index ] ) {
801+
802+
// If we are looping and slide with that index already exists, then reuse it
803+
if ( self.opts.loop && self.group.length > 2 ) {
804+
for (var key in self.slides) {
805+
if ( self.slides[ key ].index === index ) {
806+
found = self.slides[ key ];
807+
found.pos = pos;
808+
809+
self.slides[ pos ] = found;
810+
811+
delete self.slides[ key ];
812+
813+
self.updateSlide( found );
814+
815+
return found;
816+
}
817+
}
818+
}
819+
802820
$slide = $('<div class="fancybox-slide"></div>').appendTo( self.$refs.slider );
803821

804822
self.slides[ pos ] = $.extend( true, {}, self.group[ index ], {
@@ -810,6 +828,8 @@
810828

811829
}
812830

831+
return self.slides[ pos ];
832+
813833
},
814834

815835
zoomInOut : function( type, duration, callback ) {
@@ -908,24 +928,25 @@
908928
$.fancybox.setTranslate( $what, end );
909929

910930
setTimeout(function() {
911-
var reset;
931+
requestAFrame(function() {
932+
var reset;
912933

913-
$what.css( 'transition', 'none' );
934+
$what.css( 'transition', 'none' );
914935

915-
reset = $.fancybox.getTranslate( $what );
936+
reset = $.fancybox.getTranslate( $what );
916937

917-
reset.scaleX = 1;
918-
reset.scaleY = 1;
938+
reset.scaleX = 1;
939+
reset.scaleY = 1;
919940

920-
// Reset scalex/scaleY values; this helps for perfomance
921-
$.fancybox.setTranslate( $what, reset );
941+
// Reset scalex/scaleY values; this helps for perfomance
942+
$.fancybox.setTranslate( $what, reset );
922943

923-
self.trigger( 'afterZoom' + type );
944+
self.trigger( 'afterZoom' + type );
924945

925-
callback.apply( self );
926-
927-
self.isAnimating = false;
946+
callback.apply( self );
928947

948+
self.isAnimating = false;
949+
});
929950
}, duration);
930951

931952
});
@@ -1204,14 +1225,21 @@
12041225

12051226
var self = this;
12061227
var $what = slide.$placeholder;
1228+
var leftPos;
12071229

12081230
slide = slide || self.current;
12091231

12101232
if ( !slide || self.isClosing ) {
12111233
return;
12121234
}
12131235

1214-
$.fancybox.setTranslate( slide.$slide, { top: 0, left : ( slide.pos * Math.floor( slide.$slide.width() ) ) + ( slide.pos * slide.opts.gutter) } );
1236+
leftPos = ( slide.pos * Math.floor( slide.$slide.width() ) ) + ( slide.pos * slide.opts.gutter);
1237+
1238+
if ( leftPos !== slide.leftPos ) {
1239+
$.fancybox.setTranslate( slide.$slide, { top: 0, left : leftPos } );
1240+
1241+
slide.leftPos = leftPos;
1242+
}
12151243

12161244
if ( andContent !== false && $what ) {
12171245
$.fancybox.setTranslate( $what, self.getFitPos( slide ) );
@@ -1485,7 +1513,6 @@
14851513
// ======================
14861514

14871515
setBigImage : function ( slide ) {
1488-
14891516
var self = this;
14901517
var $img = $('<img />');
14911518

@@ -1544,7 +1571,13 @@
15441571
}
15451572

15461573
if ( slide.opts.image.protect ) {
1547-
$('<div class="fancybox-spaceball"></div>').appendTo( slide.$placeholder );
1574+
$('<div class="fancybox-spaceball"></div>').appendTo( slide.$placeholder ).on('contextmenu.fb',function(e){
1575+
if ( e.button == 2 ) {
1576+
e.preventDefault();
1577+
}
1578+
1579+
return true;
1580+
});
15481581
}
15491582

15501583
},
@@ -1639,8 +1672,8 @@
16391672
// it will fail if frame is not with the same origin
16401673

16411674
try {
1642-
$contents = $iframe.contents();
1643-
$body = $contents.find('body');
1675+
$contents = $iframe.contents();
1676+
$body = $contents.find('body');
16441677

16451678
} catch (ignore) {}
16461679

@@ -1895,7 +1928,8 @@
18951928

18961929
self.trigger( 'onComplete' );
18971930

1898-
if ( current.opts.focus ) {
1931+
// Try to focus on the first focusable element, skip for images and iframes
1932+
if ( current.opts.focus && ( current.type === 'image' || current.type === 'iframe' ) ) {
18991933
self.focus();
19001934
}
19011935

@@ -1934,7 +1968,10 @@
19341968

19351969
focus : function() {
19361970

1937-
var $el = this.current && this.current.isComplete ? this.current.$slide.find('button,:input,[tabindex],a:not(".disabled")').filter(':visible:first') : null;
1971+
var current = this.current;
1972+
var $el;
1973+
1974+
$el = current && current.isComplete ? current.$slide.find('button,:input,[tabindex],a:not(".disabled")').filter(':visible:first') : null;
19381975

19391976
if ( !$el || !$el.length ) {
19401977
$el = this.$refs.container;
@@ -1947,8 +1984,8 @@
19471984
this.$refs.slider_wrap.scrollLeft(0);
19481985

19491986
// And the same goes for slide element
1950-
if ( this.current ) {
1951-
this.current.$slide.scrollTop(0);
1987+
if ( current ) {
1988+
current.$slide.scrollTop(0);
19521989
}
19531990
},
19541991

@@ -2226,7 +2263,7 @@
22262263

22272264
$.fancybox = {
22282265

2229-
version : "3.0.29",
2266+
version : "3.0.30",
22302267
defaults : defaults,
22312268

22322269

dist/jquery.fancybox.min.js

Lines changed: 3 additions & 3 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,7 +1,7 @@
11
{
22
"name": "fancybox",
33
"description": "Touch enabled, responsive and fully customizable jQuery lightbox script",
4-
"version": "3.0.29",
4+
"version": "3.0.30",
55
"homepage": "http://fancyapps.com/fancybox/",
66
"main": "./dist/jquery.fancybox.js",
77
"author": "fancyApps",

0 commit comments

Comments
 (0)