@@ -92,18 +92,17 @@ export class LazyCore {
9292 this . io = new IntersectionObserver ( entries => {
9393 entries . forEach ( item => {
9494 if ( item . isIntersecting ) {
95- const src = ( item . target as LazyElement ) . dataset ?. src
96- const srcset = ( item . target as LazyElement ) . dataset ?. srcset
97- const bg = ( item . target as LazyElement ) . dataset ?. bg
95+ const el = ( item . target as LazyElement )
96+ const { src, srcset, bg } = getDataset ( el )
9897
9998 if ( src ) {
100- ( item . target as LazyElement ) . src = src
99+ el . src = src
101100 }
102101 if ( srcset ) {
103- ( item . target as LazyElement ) . srcset = srcset
102+ el . srcset = srcset
104103 }
105104 if ( bg ) {
106- ( item . target as LazyElement ) . style . backgroundImage = `url(${ bg } )`
105+ el . style . backgroundImage = `url(${ bg } )`
107106 }
108107 this . io ?. unobserve ( item . target )
109108 }
@@ -114,6 +113,28 @@ export class LazyCore {
114113 }
115114}
116115
116+ function getDataset ( el : LazyElement ) {
117+ if ( el . dataset ) {
118+ return el . dataset
119+ } else {
120+ const obj = { } as LazyElement [ 'dataset' ]
121+
122+ for ( let i = 0 ; i < el . attributes . length ; i ++ ) {
123+ const name = el . attributes [ i ] . nodeName
124+
125+ if ( / ^ d a t a - \w + $ / . test ( name ) ) {
126+ const key = name . split ( '-' ) [ 1 ]
127+ const value = el . attributes [ i ] . nodeValue || undefined
128+
129+ if ( [ 'src' , 'srcset' , 'bg' ] . indexOf ( key ) !== - 1 ) {
130+ obj [ key as 'src' | 'srcset' | 'bg' ] = value
131+ }
132+ }
133+ }
134+ return obj
135+ }
136+ }
137+
117138export function getVueVersion ( Vue : any ) {
118139 return Number ( Vue . version . split ( '.' ) [ 0 ] )
119140}
0 commit comments