Skip to content

Commit 59ea3bb

Browse files
committed
added option to avoid preload in image block
1 parent 584e2a9 commit 59ea3bb

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/blocks/image/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const props = ['scale', 'filter', 'position', 'size']
55

66
const image = function (el, config) {
77
const url = config.url
8+
const noPreload = config.noPreload
89
if (!url) return false
910

1011
const that = this
@@ -24,14 +25,22 @@ const image = function (el, config) {
2425
const wrapper = el.querySelector('.' + css.preimg)
2526

2627
const img = new Image()
27-
img.onload = () => {
28+
img.src = url
29+
30+
if (noPreload) {
2831
wrapper.appendChild(img)
29-
resolve(that)
32+
setTimeout(() => {
33+
resolve(that)
34+
}, 1)
35+
} else {
36+
img.onload = () => {
37+
wrapper.appendChild(img)
38+
resolve(that)
39+
}
40+
img.onerror = () => {
41+
resolve(that)
42+
}
3043
}
31-
img.onerror = () => {
32-
resolve(that)
33-
}
34-
img.src = url
3544
})
3645
}
3746

0 commit comments

Comments
 (0)