File tree Expand file tree Collapse file tree 6 files changed +68
-11
lines changed
Expand file tree Collapse file tree 6 files changed +68
-11
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,18 @@ Create _a broken_ picture element structure.
236236
237237When _ lozad_ loads this picture element, it will fix it.
238238
239+ If you want to use image placeholder (like low quality image placeholder), you can set a temporary ` img ` tag inside your ` picture ` tag. It will be removed when _ lozad_ loads the picture element.
240+
241+ ``` html
242+ <picture class =" lozad" style =" display : block ; min-height : 1rem " data-iesrc =" images/thumbs/04.jpg" data-alt =" " >
243+ <source srcset =" images/thumbs/04.jpg" media =" (min-width: 1280px)" >
244+ <source srcset =" images/thumbs/05.jpg" media =" (min-width: 980px)" >
245+ <source srcset =" images/thumbs/06.jpg" media =" (min-width: 320px)" >
246+ <!-- you can define a low quality image placeholder that will be removed when the picture is loaded -->
247+ <img src =" data:image/jpeg;base64,/some_lqip_in_base_64==" />
248+ </picture >
249+ ```
250+
239251## Example with video
240252
241253``` html
Original file line number Diff line number Diff line change 1- /*! lozad.js - v1.15.0 - 2020-08-01
1+ /*! lozad.js - v1.15.0 - 2020-09-05
22* https://github.com/ApoorvSaxena/lozad.js
33* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */
44
@@ -15,7 +15,14 @@ const defaultConfig = {
1515 threshold : 0 ,
1616 load ( element ) {
1717 if ( element . nodeName . toLowerCase ( ) === 'picture' ) {
18- const img = document . createElement ( 'img' ) ;
18+ let img = element . querySelector ( 'img' ) ;
19+ let append = false ;
20+
21+ if ( img === null ) {
22+ img = document . createElement ( 'img' ) ;
23+ append = true ;
24+ }
25+
1926 if ( isIE && element . getAttribute ( 'data-iesrc' ) ) {
2027 img . src = element . getAttribute ( 'data-iesrc' ) ;
2128 }
@@ -24,7 +31,9 @@ const defaultConfig = {
2431 img . alt = element . getAttribute ( 'data-alt' ) ;
2532 }
2633
27- element . append ( img ) ;
34+ if ( append ) {
35+ element . append ( img ) ;
36+ }
2837 }
2938
3039 if ( element . nodeName . toLowerCase ( ) === 'video' && ! element . getAttribute ( 'data-src' ) ) {
Original file line number Diff line number Diff line change 1- /*! lozad.js - v1.15.0 - 2020-08-01
1+ /*! lozad.js - v1.15.0 - 2020-09-05
22* https://github.com/ApoorvSaxena/lozad.js
33* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */
44
2121 threshold : 0 ,
2222 load : function load ( element ) {
2323 if ( element . nodeName . toLowerCase ( ) === 'picture' ) {
24- var img = document . createElement ( 'img' ) ;
24+ var img = element . querySelector ( 'img' ) ;
25+ var append = false ;
26+
27+ if ( img === null ) {
28+ img = document . createElement ( 'img' ) ;
29+ append = true ;
30+ }
31+
2532 if ( isIE && element . getAttribute ( 'data-iesrc' ) ) {
2633 img . src = element . getAttribute ( 'data-iesrc' ) ;
2734 }
3037 img . alt = element . getAttribute ( 'data-alt' ) ;
3138 }
3239
33- element . append ( img ) ;
40+ if ( append ) {
41+ element . append ( img ) ;
42+ }
3443 }
3544
3645 if ( element . nodeName . toLowerCase ( ) === 'video' && ! element . getAttribute ( 'data-src' ) ) {
Original file line number Diff line number Diff line change @@ -10,7 +10,14 @@ const defaultConfig = {
1010 threshold : 0 ,
1111 load ( element ) {
1212 if ( element . nodeName . toLowerCase ( ) === 'picture' ) {
13- const img = document . createElement ( 'img' )
13+ let img = element . querySelector ( 'img' )
14+ let append = false
15+
16+ if ( img === null ) {
17+ img = document . createElement ( 'img' )
18+ append = true
19+ }
20+
1421 if ( isIE && element . getAttribute ( 'data-iesrc' ) ) {
1522 img . src = element . getAttribute ( 'data-iesrc' )
1623 }
@@ -19,7 +26,9 @@ const defaultConfig = {
1926 img . alt = element . getAttribute ( 'data-alt' )
2027 }
2128
22- element . append ( img )
29+ if ( append ) {
30+ element . append ( img )
31+ }
2332 }
2433
2534 if ( element . nodeName . toLowerCase ( ) === 'video' && ! element . getAttribute ( 'data-src' ) ) {
Original file line number Diff line number Diff line change @@ -336,6 +336,24 @@ describe('lozad', () => {
336336 const img = picture . children [ 1 ]
337337 assert . strictEqual ( 'alt text' , img . getAttribute ( 'alt' ) )
338338 } )
339+
340+ it ( 'should use img tag if inside the picture' , ( ) => {
341+ const className = 'test-class'
342+ const observer = lozad ( '.' + className )
343+ const picture = document . querySelectorAll ( 'picture' ) [ 0 ]
344+ picture . setAttribute ( 'class' , className )
345+
346+ const initialImage = document . createElement ( 'img' )
347+ initialImage . setAttribute ( 'customAttribute' , 'custom value' )
348+ picture . appendChild ( initialImage )
349+ observer . observe ( )
350+
351+ const img = picture . children [ 1 ]
352+ assert . strictEqual (
353+ initialImage . getAttribute ( 'customAttribute' ) ,
354+ img . getAttribute ( 'customAttribute' )
355+ )
356+ } )
339357 } )
340358
341359 describe ( 'toggle class' , ( ) => {
You can’t perform that action at this time.
0 commit comments