Skip to content

Commit 353f8be

Browse files
committed
feat: suppert loading="eager"
1 parent ff542ed commit 353f8be

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ import LazyLoading from 'vue-lazy-loading'
3030
Vue.use(LazyLoading)
3131
```
3232

33-
- **Setting a fixed size is better for browser loading**
34-
3533
``` vue
3634
<template>
35+
<!-- Setting a fixed size is better for browser loading -->
36+
<!-- Replace `src` with `v-lazy` -->
3737
<img v-lazy="'img.jpg'" width="536" height="354" />
3838
39+
<!-- Set `loading="lazy"` is not required -->
40+
<iframe v-lazy="'iframe.html'" loading="lazy" width="1000" height="500" />
41+
42+
<!-- Load right away with set `loading="eager"` -->
43+
<iframe v-lazy="'iframe.html'" loading="eager" width="1000" height="500" />
44+
45+
<!-- Pass in the Relative URLs like this -->
3946
<img v-lazy="logo" width="100" height="100" />
4047
41-
<iframe v-lazy="'iframe.html'" width="1000" height="500" />
48+
<!-- Replace `srcset` with `v-lazy:set` or `v-lazy:srcset` -->
49+
<img v-lazy="'img.jpg'" v-lazy:set="'img.jpg 1000w, img-2x.jpg 2000w'" width="536" height="354" />
4250
51+
<!-- Replace `style.backgroundImage` with `v-lazy:bg` or `v-lazy:background` -->
4352
<div v-lazy:bg="logo">background</div>
44-
<!--or v-lazy:background="logo"-->
45-
46-
<img v-lazy="'img.jpg'" v-lazy:set="'img.jpg 1000w, img-2x.jpg 2000w'" width="536" height="354" />
47-
<!--or v-lazy:srcset="URL"-->
4853
</template>
4954
5055
<script>
@@ -77,7 +82,7 @@ rootMargin for IntersectionObserver
7782

7883
## Browser Support
7984

80-
Available in [latest browsers](http://caniuse.com/#feat=intersectionobserver). If browser support is not available, then make use of this [polyfill](https://www.npmjs.com/package/intersection-observer).
85+
Available in [latest browsers](http://caniuse.com/#feat=intersectionobserver). If browser support is not available (eg IE), then make use of this [polyfill](https://www.npmjs.com/package/intersection-observer).
8186

8287
``` js
8388
require('intersection-observer')

src/util.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ export class LazyCore {
2525
}
2626

2727
bind(el: Element, binding: LazyBinding) {
28-
!el.hasAttribute('loading') && el.setAttribute('loading', 'lazy')
28+
binding.arg !== 'bg' &&
29+
binding.arg !== 'background' &&
30+
!el.hasAttribute('loading') &&
31+
el.setAttribute('loading', 'lazy')
2932
this.update(el, binding)
3033
}
3134

3235
update(el: Element, { oldValue, value, arg }: LazyBinding) {
3336
if (oldValue === value) return
37+
const isEager = el.getAttribute('loading') === 'eager'
38+
3439
if (arg) {
3540
switch (arg) {
3641
case 'bg':
3742
case 'background':
3843
if ((el as LazyElement).style.backgroundImage) {
3944
(el as LazyElement).style.backgroundImage = ''
4045
}
41-
if (this.type === 'loading' || this.type === 'observer') {
46+
if (!isEager && (this.type === 'loading' || this.type === 'observer')) {
4247
if (!this.io) {
4348
this.setObserver()
4449
}
@@ -53,22 +58,22 @@ export class LazyCore {
5358
el.hasAttribute('srcset') && el.removeAttribute('srcset')
5459
if (this.type === 'loading') {
5560
el.setAttribute('srcset', value)
56-
} else if (this.type === 'observer') {
61+
} else if (!isEager && this.type === 'observer') {
5762
el.setAttribute('data-srcset', value)
5863
this.io?.observe(el)
5964
} else {
6065
el.setAttribute('srcset', value)
6166
}
6267
break
6368
default:
64-
error('One of [v-lazy="URL", v-lazy:bg="URL", v-lazy:background="URL", v-lazy:set="URL"]')
69+
error('One of [v-lazy="URL", v-lazy:bg="URL", v-lazy:background="URL", v-lazy:set="URL", v-lazy:srcset="URL"]')
6570
break;
6671
}
6772
} else {
6873
el.hasAttribute('src') && el.removeAttribute('src')
6974
if (this.type === 'loading') {
7075
el.setAttribute('src', value)
71-
} else if (this.type === 'observer') {
76+
} else if (!isEager && this.type === 'observer') {
7277
el.setAttribute('data-src', value)
7378
this.io?.observe(el)
7479
} else {

0 commit comments

Comments
 (0)