Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lib
npm-debug.log
package-lock.json
.idea
.history
2 changes: 1 addition & 1 deletion src/lazy-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const LazyImage = (lazyManager) => {
return
}
const src = this.options.src
loadImageAsync({ src }, ({ src }) => {
loadImageAsync({ src, el: this.$el }, ({ src }) => {
this.renderSrc = src
this.state.loaded = true
}, e => {
Expand Down
6 changes: 4 additions & 2 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export default class ReactiveListener {
this.state.loading = true
loadImageAsync({
src: this.loading,
cors: this.cors
cors: this.cors,
el: this.el
}, data => {
this.render('loading', false)
this.state.loading = false
Expand Down Expand Up @@ -163,7 +164,8 @@ export default class ReactiveListener {

loadImageAsync({
src: this.src,
cors: this.cors
cors: this.cors,
el: this.el
}, data => {
this.naturalHeight = data.naturalHeight
this.naturalWidth = data.naturalWidth
Expand Down
8 changes: 7 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,18 @@ const _ = {
}

const loadImageAsync = (item, resolve, reject) => {
const attributes = item.el.attributes
let image = new Image()
if (!item || !item.src) {
const err = new Error('image src is required')
return reject(err)
}

// 将图片预配置的attributes赋值给image实例
for (let i = 0; i < attributes.length; i++) {
if (!['id', 'class', 'src'].includes(attributes[i].name)) {
image.setAttribute(attributes[i].name, attributes[i].value)
}
}
image.src = item.src
if (item.cors) {
image.crossOrigin = item.cors
Expand Down
Loading