vue-lazyload的demo源码
时间: 2024-05-05 17:16:32 浏览: 8
由于Vue-Lazyload是一个Vue.js插件,因此需要在Vue.js项目中使用。以下是一个使用Vue-Lazyload的demo源码:
index.html:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue-Lazyload Demo</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item, index) in list" :key="index">
<img v-lazy="item.imgUrl" alt="">
<p>{{ item.title }}</p>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-lazyload"></script>
<script src="app.js"></script>
</body>
</html>
```
app.js:
```javascript
Vue.use(VueLazyload)
var app = new Vue({
el: '#app',
data: {
list: [
{
title: 'Image 1',
imgUrl: 'https://picsum.photos/id/1001/500/300'
},
{
title: 'Image 2',
imgUrl: 'https://picsum.photos/id/1002/500/300'
},
{
title: 'Image 3',
imgUrl: 'https://picsum.photos/id/1003/500/300'
},
{
title: 'Image 4',
imgUrl: 'https://picsum.photos/id/1004/500/300'
},
{
title: 'Image 5',
imgUrl: 'https://picsum.photos/id/1005/500/300'
},
{
title: 'Image 6',
imgUrl: 'https://picsum.photos/id/1006/500/300'
},
{
title: 'Image 7',
imgUrl: 'https://picsum.photos/id/1007/500/300'
},
{
title: 'Image 8',
imgUrl: 'https://picsum.photos/id/1008/500/300'
},
{
title: 'Image 9',
imgUrl: 'https://picsum.photos/id/1009/500/300'
},
{
title: 'Image 10',
imgUrl: 'https://picsum.photos/id/1010/500/300'
}
]
}
})
```
上述代码中,我们使用了Vue.use()方法来安装Vue-Lazyload插件。在Vue实例中,我们定义了一个包含图片信息的列表,使用v-for指令渲染到页面中。在<img>标签上添加v-lazy指令,指向列表中的图片链接,表示该图片需要懒加载。最后,我们使用new Vue()方法创建Vue实例,并将其挂载到id为app的DOM元素上。