Skip to content

Check if image is fully loaded #79

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 6 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
13 changes: 11 additions & 2 deletions dist/lozad.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! lozad.js - v1.0.9 - 2017-10-26
/*! lozad.js - v1.0.9 - 2017-12-16
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2017 Apoorv Saxena; Licensed MIT */

Expand Down Expand Up @@ -28,7 +28,16 @@ var defaultConfig = {
};

function markAsLoaded(element) {
element.setAttribute('data-loaded', true);
var imgLoad = new Image();
imgLoad.onload = function () {
element.setAttribute('data-loaded', true);
};
if (element.getAttribute('data-src')) {
imgLoad.src = element.getAttribute('src');
}
if (element.getAttribute('data-background-image')) {
imgLoad.src = element.getAttribute('data-background-image');
}
}

var isLoaded = function isLoaded(element) {
Expand Down
4 changes: 2 additions & 2 deletions dist/lozad.min.js

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

11 changes: 10 additions & 1 deletion src/lozad.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ const defaultConfig = {
}

function markAsLoaded(element) {
element.setAttribute('data-loaded', true)
const imgLoad = new Image()
imgLoad.onload = () => {
element.setAttribute('data-loaded', true)
}
if (element.getAttribute('data-src')) {
imgLoad.src = element.getAttribute('src')
}
if (element.getAttribute('data-background-image')) {
imgLoad.src = element.getAttribute('data-background-image')
}
}

const isLoaded = element => element.getAttribute('data-loaded') === 'true'
Expand Down
17 changes: 13 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ describe('lozad', () => {
const observer = lozad()
const image = document.getElementsByTagName('img')[0]
observer.observe()
assert.equal('true', image.dataset.loaded)
assert.equal(image.getAttribute('src'), image.dataset.src)
const imgLoad = new Image()
imgLoad.onload = () => {
image.setAttribute('data-loaded', true)
assert.equal('true', image.dataset.loaded)
assert.equal(image.getAttribute('src'), image.dataset.src)
}
imgLoad.src = image.getAttribute('src')
})
})

Expand All @@ -81,8 +86,12 @@ describe('lozad', () => {
const image = document.getElementsByTagName('img')[0]
image.setAttribute('class', className)
observer.observe()
assert.equal('true', image.dataset.loaded)
assert.equal(image.getAttribute('src'), image.dataset.src)
const imgLoad = new Image()
imgLoad.onload = () => {
image.setAttribute('data-loaded', true)
assert.equal('true', image.dataset.loaded)
assert.equal(image.getAttribute('src'), image.dataset.src)
}
})
})

Expand Down