Skip to content

Commit ce001b0

Browse files
author
Mateusz Krzeszowiak
committed
Prevent product image from blinking when gallery loads
1 parent 87b7cba commit ce001b0

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

lib/web/mage/gallery/gallery.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ define([
270270
next: $t('Next'),
271271
previous: $t('Previous')
272272
}),
273-
mainImageIndex;
273+
mainImageIndex,
274+
$element = settings.$element,
275+
$placeholderImage = $element.find('.gallery-placeholder__image'),
276+
$fotoramaElement;
274277

275278
if (settings.breakpoints) {
276279
_.each(_.values(settings.breakpoints), function (breakpoint) {
@@ -285,23 +288,40 @@ define([
285288
settings.breakpoints = breakpoints;
286289
}
287290

288-
_.extend(config, config.options);
289-
config.options = undefined;
290-
291-
config.click = false;
292-
config.breakpoints = null;
291+
_.extend(config, config.options,
292+
{
293+
options: undefined,
294+
click: false,
295+
breakpoints: null
296+
}
297+
);
293298
settings.currentConfig = config;
294-
settings.$element.css('min-height', settings.$element.height());
295-
settings.$element.html(tpl);
296-
settings.$element.removeClass('_block-content-loading');
297-
settings.$elementF = $(settings.$element.children()[0]);
298-
settings.$elementF.fotorama(config);
299-
settings.$element.css('min-height', '');
300-
settings.fotoramaApi = settings.$elementF.data('fotorama');
299+
300+
// Adjust wrapper and placeholder styling just before initializing the gallery.
301+
$placeholderImage.css({
302+
position: 'absolute',
303+
left: '50%',
304+
transform: 'translateX(-50%)'
305+
});
306+
$element
307+
.css('min-height', settings.$element.height())
308+
.append(tpl);
309+
310+
$fotoramaElement = $element.find('[data-gallery-role="gallery"]');
311+
$fotoramaElement.fotorama(config);
312+
$fotoramaElement.find('.fotorama__stage__frame.fotorama__active')
313+
.one('f:load', function() {
314+
// Remove placeholder when main gallery image loads.
315+
$placeholderImage.remove();
316+
$element
317+
.removeClass('_block-content-loading')
318+
.css('min-height', '');
319+
});
320+
settings.fotoramaApi = $fotoramaElement.data('fotorama');
321+
301322
$.extend(true, config, this.startConfig);
302323

303324
mainImageIndex = getMainImageIndex(config.data);
304-
305325
if (mainImageIndex) {
306326
this.settings.fotoramaApi.show({
307327
index: mainImageIndex,

0 commit comments

Comments
 (0)