Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add color placeholder feature ✨
  • Loading branch information
igorkamyshev committed Jul 2, 2020
1 parent 4509b99 commit 2aca9ca
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -202,6 +202,16 @@ const coolImage = document.querySelector('.image-to-load-first')
observer.triggerLoad(coolImage);
```

### Large image improvment

Sometimes image loading takes a long time. For this case, you can add a placeholder color:

```html
<img class="lozad" data-placeholder-color="red" data-src="image.png" />
```

Lozad set the background of `img` element and users will see the fallback.

## Example with picture tag

Create _a broken_ picture element structure.
Expand Down
1 change: 1 addition & 0 deletions demo/assets/css/main.css
Expand Up @@ -47,6 +47,7 @@ body {

img {
width: 100%;
height: 100%;
}

/* Box Model */
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Expand Up @@ -58,7 +58,7 @@ <h1><a href="https://github.com/ApoorvSaxena/lozad.js"><strong>Lozad.js</strong>
<h1>Lozad.js - Performant Lazy Loading Library</h1>
</article>
<article class="thumb">
<img class="lozad" data-src="images/thumbs/01.jpg" data-index="1" />
<img class="lozad" data-placeholder-color="#583624" data-src="images/thumbs/01.jpg" data-index="1" />
<noscript><img src="images/thumbs/01.jpg" data-index="1" /></noscript>
<h2>Image 1</h2>
</article>
Expand Down
13 changes: 12 additions & 1 deletion dist/lozad.es.js
@@ -1,4 +1,4 @@
/*! lozad.js - v1.15.0 - 2020-05-23
/*! lozad.js - v1.15.0 - 2020-07-02
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */

Expand Down Expand Up @@ -83,6 +83,12 @@ function markAsLoaded(element) {
element.setAttribute('data-loaded', true);
}

function preLoad(element) {
if (element.getAttribute('data-placeholder-color')) {
element.style.backgroundColor = element.getAttribute('data-placeholder-color');
}
}

const isLoaded = element => element.getAttribute('data-loaded') === 'true';

const onIntersection = (load, loaded) => (entries, observer) => {
Expand Down Expand Up @@ -123,6 +129,11 @@ function lozad (selector = '.lozad', options = {}) {
});
}

const elements = getElements(selector, root);
for (let i = 0; i < elements.length; i++) {
preLoad(elements[i]);
}

return {
observe() {
const elements = getElements(selector, root);
Expand Down
25 changes: 18 additions & 7 deletions dist/lozad.js
@@ -1,4 +1,4 @@
/*! lozad.js - v1.15.0 - 2020-05-23
/*! lozad.js - v1.15.0 - 2020-07-02
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */

Expand Down Expand Up @@ -89,6 +89,12 @@
element.setAttribute('data-loaded', true);
}

function preLoad(element) {
if (element.getAttribute('data-placeholder-color')) {
element.style.backgroundColor = element.getAttribute('data-placeholder-color');
}
}

var isLoaded = function isLoaded(element) {
return element.getAttribute('data-loaded') === 'true';
};
Expand Down Expand Up @@ -144,23 +150,28 @@
});
}

var elements = getElements(selector, root);
for (var i = 0; i < elements.length; i++) {
preLoad(elements[i]);
}

return {
observe: function observe() {
var elements = getElements(selector, root);

for (var i = 0; i < elements.length; i++) {
if (isLoaded(elements[i])) {
for (var _i = 0; _i < elements.length; _i++) {
if (isLoaded(elements[_i])) {
continue;
}

if (observer) {
observer.observe(elements[i]);
observer.observe(elements[_i]);
continue;
}

load(elements[i]);
markAsLoaded(elements[i]);
loaded(elements[i]);
load(elements[_i]);
markAsLoaded(elements[_i]);
loaded(elements[_i]);
}
},
triggerLoad: function triggerLoad(element) {
Expand Down
6 changes: 3 additions & 3 deletions dist/lozad.min.js

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

11 changes: 11 additions & 0 deletions src/lozad.js
Expand Up @@ -78,6 +78,12 @@ function markAsLoaded(element) {
element.setAttribute('data-loaded', true)
}

function preLoad(element) {
if (element.getAttribute('data-placeholder-color')) {
element.style.backgroundColor = element.getAttribute('data-placeholder-color')
}
}

const isLoaded = element => element.getAttribute('data-loaded') === 'true'

const onIntersection = (load, loaded) => (entries, observer) => {
Expand Down Expand Up @@ -118,6 +124,11 @@ export default function (selector = '.lozad', options = {}) {
})
}

const elements = getElements(selector, root)
for (let i = 0; i < elements.length; i++) {
preLoad(elements[i])
}

return {
observe() {
const elements = getElements(selector, root)
Expand Down

0 comments on commit 2aca9ca

Please sign in to comment.