Skip to content

Commit 4aa360a

Browse files
authored
Merge pull request #3 from tolking/dev
fix dataset is undefined on IE
2 parents 8eecdbb + c9d7d5a commit 4aa360a

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
node_modules/
99
test/
1010
docs/
11-
examples/
11+
example/
12+
src/
1213

1314
npm-debug.log
1415
yarn-error.log

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import LazyLoading from 'vue-lazy-loading'
3030
Vue.use(LazyLoading)
3131
```
3232

33-
``` vue
33+
``` html
3434
<template>
3535
<!-- Setting a fixed size is better for browser loading -->
3636
<!-- Replace `src` with `v-lazy` -->
@@ -103,7 +103,3 @@ import LazyLoading from 'vue-lazy-loading'
103103
## License
104104
105105
[MIT](http://opensource.org/licenses/MIT)
106-
107-
## Keywords
108-
109-
vue lazy img iframe loading background-image

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "vue-lazy-loading",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "a vue plugin to better supporting lazy loading for image and iframe",
55
"main": "dist/index.js",
6-
"typings": "dist/index.d.ts",
6+
"type": "module",
7+
"types": "dist/index.d.ts",
78
"scripts": {
8-
"dev": "tsc -w"
9+
"dev": "tsc -w",
10+
"build": "tsc -b"
911
},
1012
"repository": {
1113
"type": "git",

src/util.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,17 @@ export class LazyCore {
9292
this.io = new IntersectionObserver(entries => {
9393
entries.forEach(item => {
9494
if (item.isIntersecting) {
95-
const src = (item.target as LazyElement).dataset?.src
96-
const srcset = (item.target as LazyElement).dataset?.srcset
97-
const bg = (item.target as LazyElement).dataset?.bg
95+
const el = (item.target as LazyElement)
96+
const { src, srcset, bg } = getDataset(el)
9897

9998
if (src) {
100-
(item.target as LazyElement).src = src
99+
el.src = src
101100
}
102101
if (srcset) {
103-
(item.target as LazyElement).srcset = srcset
102+
el.srcset = srcset
104103
}
105104
if (bg) {
106-
(item.target as LazyElement).style.backgroundImage = `url(${bg})`
105+
el.style.backgroundImage = `url(${bg})`
107106
}
108107
this.io?.unobserve(item.target)
109108
}
@@ -114,6 +113,28 @@ export class LazyCore {
114113
}
115114
}
116115

116+
function getDataset(el: LazyElement) {
117+
if (el.dataset) {
118+
return el.dataset
119+
} else {
120+
const obj = {} as LazyElement['dataset']
121+
122+
for (let i = 0; i < el.attributes.length; i++) {
123+
const name = el.attributes[i].nodeName
124+
125+
if (/^data-\w+$/.test(name)) {
126+
const key = name.split('-')[1]
127+
const value = el.attributes[i].nodeValue || undefined
128+
129+
if (['src', 'srcset', 'bg'].indexOf(key) !== -1) {
130+
obj[key as 'src' | 'srcset' | 'bg'] = value
131+
}
132+
}
133+
}
134+
return obj
135+
}
136+
}
137+
117138
export function getVueVersion(Vue: any) {
118139
return Number(Vue.version.split('.')[0])
119140
}

0 commit comments

Comments
 (0)