Skip to content

Makes lazy loading a Picture element more simple (IMHO) #145

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

Open
wants to merge 1 commit 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
36 changes: 28 additions & 8 deletions dist/lozad.es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! lozad.js - v1.7.0 - 2018-11-08
/*! lozad.js - v1.7.0 - 2018-11-10
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2018 Apoorv Saxena; Licensed MIT */

Expand All @@ -15,14 +15,34 @@ const defaultConfig = {
threshold: 0,
load(element) {
if (element.nodeName.toLowerCase() === 'picture') {
const img = document.createElement('img');
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc');
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt');
const imgEl = element.querySelector('img');
if (imgEl === null) { // Check to see if there isn't already an img tag
const img = document.createElement('img');
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc');
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt');
}
element.appendChild(img);
} else {
// Gets an array of source elements
// Node list converted to array because some browsers don't support forEach on a node list
const sourceElements = [...element.querySelectorAll('source')];
// Loop thrrough them all
sourceElements.forEach(source => {
// If there is a data-srcset attribute, make it a srcset attribute
if (source.getAttribute('data-srcset')) {
source.setAttribute('srcset', source.getAttribute('data-srcset'));
}
});
if (imgEl.getAttribute('data-src')) {
imgEl.src = imgEl.getAttribute('data-src');
}
if (imgEl.getAttribute('data-srcset')) {
imgEl.setAttribute('srcset', imgEl.getAttribute('data-srcset'));
}
}
element.appendChild(img);
}
if (element.getAttribute('data-src')) {
element.src = element.getAttribute('data-src');
Expand Down
39 changes: 31 additions & 8 deletions dist/lozad.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! lozad.js - v1.7.0 - 2018-11-08
/*! lozad.js - v1.7.0 - 2018-11-10
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2018 Apoorv Saxena; Licensed MIT */

Expand All @@ -11,6 +11,8 @@

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

/**
* Detect IE browser
* @const {boolean}
Expand All @@ -23,14 +25,35 @@
threshold: 0,
load: function load(element) {
if (element.nodeName.toLowerCase() === 'picture') {
var img = document.createElement('img');
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc');
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt');
var imgEl = element.querySelector('img');
if (imgEl === null) {
// Check to see if there isn't already an img tag
var img = document.createElement('img');
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc');
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt');
}
element.appendChild(img);
} else {
// Gets an array of source elements
// Node list converted to array because some browsers don't support forEach on a node list
var sourceElements = [].concat(_toConsumableArray(element.querySelectorAll('source')));
// Loop thrrough them all
sourceElements.forEach(function (source) {
// If there is a data-srcset attribute, make it a srcset attribute
if (source.getAttribute('data-srcset')) {
source.setAttribute('srcset', source.getAttribute('data-srcset'));
}
});
if (imgEl.getAttribute('data-src')) {
imgEl.src = imgEl.getAttribute('data-src');
}
if (imgEl.getAttribute('data-srcset')) {
imgEl.setAttribute('srcset', imgEl.getAttribute('data-srcset'));
}
}
element.appendChild(img);
}
if (element.getAttribute('data-src')) {
element.src = element.getAttribute('data-src');
Expand Down
13 changes: 10 additions & 3 deletions dist/lozad.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 27 additions & 7 deletions src/lozad.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@ const defaultConfig = {
threshold: 0,
load(element) {
if (element.nodeName.toLowerCase() === 'picture') {
const img = document.createElement('img')
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc')
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt')
const imgEl = element.querySelector('img')
if (imgEl === null) { // Check to see if there isn't already an img tag
const img = document.createElement('img')
if (isIE && element.getAttribute('data-iesrc')) {
img.src = element.getAttribute('data-iesrc')
}
if (element.getAttribute('data-alt')) {
img.alt = element.getAttribute('data-alt')
}
element.appendChild(img)
} else {
// Gets an array of source elements
// Node list converted to array because some browsers don't support forEach on a node list
const sourceElements = [...element.querySelectorAll('source')]
// Loop thrrough them all
sourceElements.forEach(source => {
// If there is a data-srcset attribute, make it a srcset attribute
if (source.getAttribute('data-srcset')) {
source.setAttribute('srcset', source.getAttribute('data-srcset'))
}
})
if (imgEl.getAttribute('data-src')) {
imgEl.src = imgEl.getAttribute('data-src')
}
if (imgEl.getAttribute('data-srcset')) {
imgEl.setAttribute('srcset', imgEl.getAttribute('data-srcset'))
}
}
element.appendChild(img)
}
if (element.getAttribute('data-src')) {
element.src = element.getAttribute('data-src')
Expand Down