Skip to content

Commit 8eecdbb

Browse files
authored
Merge pull request #2 from tolking/dev
- suppert loading="eager" - change README
2 parents 6de165a + 95bf7f2 commit 8eecdbb

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vue-lazy-loading
22

3-
> *alpha* a vue plugin to better supporting lazy loading for image and iframe
3+
> a vue plugin to better supporting lazy loading for image and iframe
44
55
**The plugin will preferentially use native image and iframe [lazy-loading](https://caniuse.com/#feat=loading-lazy-attr), if the browser does not support it, it will be implemented through [IntersectionObserver](https://caniuse.com/#feat=intersectionobserver)**
66

@@ -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>
@@ -75,9 +80,20 @@ Use the native image lazy-loading for the web
7580

7681
rootMargin for IntersectionObserver
7782

83+
## Load animation
84+
85+
Loading animation is not included by default. You can use it this way
86+
87+
``` css
88+
img,
89+
iframe {
90+
background: rgba(50, 50, 50, 0.3) url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32" fill="white"><path opacity=".25" d="M16 0 A16 16 0 0 0 16 32 A16 16 0 0 0 16 0 M16 4 A12 12 0 0 1 16 28 A12 12 0 0 1 16 4"/><path d="M16 0 A16 16 0 0 1 32 16 L28 16 A12 12 0 0 0 16 4z"><animateTransform attributeName="transform" type="rotate" from="0 16 16" to="360 16 16" dur="0.8s" repeatCount="indefinite" /></path></svg>') center no-repeat;
91+
}
92+
```
93+
7894
## Browser Support
7995

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).
96+
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).
8197

8298
``` js
8399
require('intersection-observer')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://github.com/tolking/vue-lazy-loading#readme",
2727
"devDependencies": {
28+
"@types/node": "^14.0.14",
2829
"typescript": "^3.9.5"
2930
}
3031
}

src/util.ts

Lines changed: 11 additions & 6 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 {
@@ -77,7 +82,7 @@ export class LazyCore {
7782
}
7883
}
7984

80-
unbind(el: Element) {
85+
unbind(el: Element) {
8186
if (this.type === 'observer') {
8287
this.io?.unobserve(el)
8388
}

0 commit comments

Comments
 (0)