Skip to content

Commit

Permalink
🔔 #1109 add <source> support for lazyLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Mar 3, 2022
1 parent 97dd3d0 commit 9565bac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 5 additions & 3 deletions js/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
const lazyAttr = 'data-flickity-lazyload';
const lazySrcAttr = `${lazyAttr}-src`;
const lazySrcsetAttr = `${lazyAttr}-srcset`;
const imgSelector = `img[${lazyAttr}], img[${lazySrcAttr}], img[${lazySrcsetAttr}]`;
const imgSelector = `img[${lazyAttr}], img[${lazySrcAttr}], ` +
`img[${lazySrcsetAttr}], source[${lazySrcsetAttr}]`;

Flickity.create.lazyLoad = function() {
this.on( 'select', this.lazyLoad );
Expand All @@ -31,7 +32,7 @@ Flickity.create.lazyLoad = function() {
let proto = Flickity.prototype;

proto.lazyLoad = function() {
let lazyLoad = this.options.lazyLoad;
let { lazyLoad } = this.options;
if ( !lazyLoad ) return;

// get adjacent cells, use lazyLoad option for adjacent count
Expand Down Expand Up @@ -108,7 +109,8 @@ LazyLoader.prototype.complete = function( event, className ) {
// unbind events
this.img.removeEventListener( 'load', this );
this.img.removeEventListener( 'error', this );
this.img.classList.add( className );
let mediaElem = this.img.parentNode.matches('picture') ? this.img.parentNode : this.img;
mediaElem.classList.add( className );

this.onComplete( this.img, event );
};
Expand Down
29 changes: 28 additions & 1 deletion sandbox/lazyload.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
background: #DDD;
}

.carousel__cell__image {
.carousel__cell__image,
.carousel__cell__image img {
display: block;
max-width: 100%;
max-height: 400px;
Expand Down Expand Up @@ -202,6 +203,32 @@ <h2>srcset</h2>
</div>
</div>

<h2>&lt;picture&gt;</h2>

<div class="carousel" data-flickity='{ "lazyLoad": true }'>
<div class="carousel__cell">
<picture class="carousel__cell__image">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/1036/1200/900" media="(min-width: 1200px)">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/1036/800/600" media="(min-width: 800px)">
<img data-flickity-lazyload-src="https://picsum.photos/id/1036/400/300">
</picture>
</div>
<div class="carousel__cell">
<picture class="carousel__cell__image">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/103/1200/900" media="(min-width: 1200px)">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/103/800/600" media="(min-width: 800px)">
<img data-flickity-lazyload-src="https://picsum.photos/id/103/400/300">
</picture>
</div>
<div class="carousel__cell">
<picture class="carousel__cell__image">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/1080/900/1200" media="(min-width: 1200px)">
<source data-flickity-lazyload-srcset="https://picsum.photos/id/1080/600/800" media="(min-width: 800px)">
<img data-flickity-lazyload-src="https://picsum.photos/id/1080/300/400">
</picture>
</div>
</div>

<!-- jQuery -->
<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src="../node_modules/jquery-bridget/jquery-bridget.js"></script>
Expand Down

0 comments on commit 9565bac

Please sign in to comment.