Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazyload picture #1109

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions js/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function getCellLazyImages( cellElem ) {
}
// select lazy images in cell
var lazySelector = 'img[data-flickity-lazyload], ' +
'img[data-flickity-lazyload-src], img[data-flickity-lazyload-srcset]';
'img[data-flickity-lazyload-src], img[data-flickity-lazyload-srcset], ' +
'source[data-flickity-lazyload-srcset]';
var imgs = cellElem.querySelectorAll( lazySelector );
return utils.makeArray( imgs );
}
Expand Down Expand Up @@ -96,7 +97,9 @@ LazyLoader.prototype.load = function() {
this.img.getAttribute('data-flickity-lazyload-src');
var srcset = this.img.getAttribute('data-flickity-lazyload-srcset');
// set src & serset
this.img.src = src;
if ( this.img.tagName.toLowerCase() === 'img' && src ) {
this.img.src = src;
}
if ( srcset ) {
this.img.setAttribute( 'srcset', srcset );
}
Expand Down
16 changes: 16 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<script src="unit/destroy.js"></script>
<script src="unit/lazyload.js"></script>
<script src="unit/lazyload-srcset.js"></script>
<script src="unit/lazyload-picture-srcset.js"></script>
<script src="unit/group-cells.js"></script>
<script src="unit/adaptive-height.js"></script>
<script src="unit/select-cell.js"></script>
Expand Down Expand Up @@ -239,6 +240,21 @@ <h2>lazyload srcset</h2>
<div class="cell"></div>
</div>

<h2>lazyload picture source srcset</h2>
<div id="lazyload-picture-srcset" class="gallery">
<div class="cell">
<picture>
<source
data-flickity-lazyload-srcset="https://via.placeholder.com/800x600.png?text=Large+1x 1x,
https://via.placeholder.com/1600x1200.png?text=Large+2x 2x" media="(min-width: 800px)">
<source
data-flickity-lazyload-srcset="https://via.placeholder.com/320x200.png?text=Small+1x 1x,
https://via.placeholder.com/640x400.png?text=Small+2x 2x" media="(max-width: 799px)">
<img data-flickity-lazyload-src="https://via.placeholder.com/800x600.png?text=Image+1+Fallback">
</picture>
</div>
</div>

<h2>groupCells</h2>
<div id="group-cells" class="gallery">
<div class="cell"></div>
Expand Down
30 changes: 30 additions & 0 deletions test/unit/lazyload-picture-srcset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
QUnit.test( 'lazyload picture srcset', function( assert ) {
'use strict';

var done = assert.async();

var gallery = document.querySelector('#lazyload-picture-srcset');
var flkty = new Flickity( gallery, {
lazyLoad: 1,
} );

var loadCount = 0;
flkty.on( 'lazyLoad', function( event, cellElem ) {
loadCount++;

assert.equal( event.type, 'load', 'event.type == load' );
assert.ok( event.target.complete, 'img ' + loadCount + ' is complete' );
assert.ok( cellElem, 'cellElement argument there' );
var sources = cellElem.querySelectorAll('source[srcset]');
assert.equal( sources.length, 2, 'All source elements of cell loaded' );
var lazyAttr = Array.from( sources ).some( function( el ) {
return el.getAttribute('data-flickity-lazyload-srcset');
} );
assert.ok( !lazyAttr, 'data-flickity-lazyload-srcset attribute removed' );

if ( loadCount == 2 ) {
done();
}
} );

} );