Skip to content

Commit 354e290

Browse files
committed
- Added: lazy load for picture srcset elements alvarotrigo#2355
1 parent 6e09dbc commit 354e290

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

jquery.fullPage.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,16 @@
16151615
$.isFunction(v.callback) && v.callback.call(this);
16161616
}
16171617

1618+
/**
1619+
* Sets the value for the given attribute from the `data-` attribute with the same suffix
1620+
* ie: data-srcset ==> srcset | data-src ==> src
1621+
*/
1622+
function setSrc(element, attribute){
1623+
element
1624+
.attr(attribute, element.data(attribute))
1625+
.removeAttr('data-' + attribute);
1626+
}
1627+
16181628
/**
16191629
* Lazy loads image, video and audio elements.
16201630
*/
@@ -1625,11 +1635,16 @@
16251635

16261636
var panel = getSlideOrSection(destiny);
16271637
var element;
1628-
1629-
panel.find('img[data-src], source[data-src], audio[data-src], iframe[data-src]').each(function(){
1638+
1639+
panel.find('img[data-src], img[data-srcset], source[data-src], audio[data-src], iframe[data-src]').each(function(){
16301640
element = $(this);
1631-
element.attr('src', element.data('src'));
1632-
element.removeAttr('data-src');
1641+
1642+
$.each(['src', 'srcset'], function(index, type){
1643+
var attribute = element.attr('data-' + type);
1644+
if(typeof attribute !== 'undefined' && attribute){
1645+
setSrc(element, type);
1646+
}
1647+
});
16331648

16341649
if(element.is('source')){
16351650
element.closest('video').get(0).load();
@@ -2757,8 +2772,11 @@
27572772

27582773
//loading all the lazy load content
27592774
container.find('img[data-src], source[data-src], audio[data-src], iframe[data-src]').each(function(){
2760-
$(this).attr('src', $(this).data('src'));
2761-
$(this).removeAttr('data-src');
2775+
setSrc($(this), 'src');
2776+
});
2777+
2778+
container.find('img[data-srcset]').each(function(){
2779+
setSrc($(this), 'srcset');
27622780
});
27632781

27642782
$(SECTION_NAV_SEL + ', ' + SLIDES_NAV_SEL + ', ' + SLIDES_ARROW_SEL).remove();

0 commit comments

Comments
 (0)