1
- import { LazyOptions , LazyElement } from '../types/index.js'
1
+ import { LazyOptions , LazyBinding , LazyElement } from '../types/index.js'
2
2
3
3
export class LazyCore {
4
4
private useNative : boolean
@@ -10,7 +10,7 @@ export class LazyCore {
10
10
this . useNative = options ?. useNative ?? true
11
11
this . rootMargin = options ?. rootMargin ?? '200px'
12
12
this . init ( )
13
- this . type = 'listener' // NOTE: test
13
+ // this.type = 'listener' // NOTE: test
14
14
15
15
console . log ( this ) ;
16
16
}
@@ -26,24 +26,46 @@ export class LazyCore {
26
26
}
27
27
}
28
28
29
- bind ( el : Element , { value } : { value : string } ) {
30
- console . log ( 'bind' , value ) ;
29
+ bind ( el : Element , binding : LazyBinding ) {
30
+ console . log ( 'bind' , binding . value ) ;
31
31
32
- ! el . getAttribute ( 'loading' ) && el . setAttribute ( 'loading' , 'lazy' )
33
- this . update ( el , { value } )
32
+ ! el . hasAttribute ( 'loading' ) && el . setAttribute ( 'loading' , 'lazy' )
33
+ this . update ( el , binding )
34
34
}
35
35
36
- update ( el : Element , { value } : { value : string } ) {
37
- console . log ( 'update' , value ) ;
36
+ update ( el : Element , { value, arg } : LazyBinding ) {
37
+ console . log ( 'update' , arg , value ) ;
38
38
39
- if ( this . type === 'loading' ) {
40
- el . setAttribute ( 'src' , value )
41
- } else if ( this . type === 'observer' ) {
42
- el . setAttribute ( 'data-src' , value )
43
- this . io ?. observe ( el )
44
- } else {
45
- console . log ( 'TODO: ' ) ;
39
+ if ( arg ) {
40
+ if ( [ 'bg' , 'background' ] . includes ( arg ) ) {
41
+ if ( this . type === 'loading' || this . type === 'observer' ) {
42
+ if ( ! this . io ) {
43
+ this . setObserver ( )
44
+ }
45
+ el . setAttribute ( 'data-bg' , value )
46
+ this . io ?. observe ( el )
47
+ } else {
48
+ console . log ( 'TODO: ' ) ;
49
+
50
+ el . setAttribute ( 'data-bg' , value )
51
+ }
52
+ } else {
53
+ error ( 'One of [v-lazy="URL", v-lazy:bg="URL", v-lazy:background="URL"]' )
54
+ }
55
+ console . log ( el . getAttribute ( 'style.backgroundImage' ) ) ;
46
56
57
+ } else {
58
+ el . hasAttribute ( 'src' ) && el . removeAttribute ( 'src' )
59
+ if ( this . type === 'loading' ) {
60
+ el . setAttribute ( 'src' , value )
61
+ } else if ( this . type === 'observer' ) {
62
+ el . setAttribute ( 'data-src' , value )
63
+ this . io ?. observe ( el )
64
+ } else {
65
+ console . log ( 'TODO: ' ) ;
66
+
67
+ el . setAttribute ( 'data-src' , value )
68
+ }
47
69
}
48
70
}
49
71
@@ -59,7 +81,15 @@ export class LazyCore {
59
81
this . io = new IntersectionObserver ( entries => {
60
82
entries . forEach ( item => {
61
83
if ( item . isIntersecting ) {
62
- ( item . target as LazyElement ) . src = ( item . target as LazyElement ) . dataset . src
84
+ const src = ( item . target as LazyElement ) . dataset ?. src
85
+ const bg = ( item . target as LazyElement ) . dataset ?. bg
86
+
87
+ if ( src ) {
88
+ ( item . target as LazyElement ) . src = src
89
+ }
90
+ if ( bg ) {
91
+ ( item . target as LazyElement ) . style . backgroundImage = `url(${ bg } )`
92
+ }
63
93
this . io ?. unobserve ( item . target )
64
94
}
65
95
} )
@@ -72,3 +102,7 @@ export class LazyCore {
72
102
export function getVueVersion ( Vue : any ) {
73
103
return Number ( Vue . version . split ( '.' ) [ 0 ] )
74
104
}
105
+
106
+ export function error ( msg : string ) {
107
+ console . error ( '[vue-lazy-loading error]: ' + msg ) ;
108
+ }
0 commit comments