当前位置: 首页 > java >正文

【Vue进阶学习笔记】实现图片懒加载

创建Vue项目

首先确保你已安装Vue CLI,然后创建一个新的Vue 3项目:

npm init vue@latest

安装依赖

安装@vueuse/core库,它提供了useIntersectionObserver组合式API:

cnpm install
cnpm install @vueuse/core

创建指令文件夹和文件

src目录下创建directives文件夹,并添加index.js文件:

// src/directives/index.js
import { useIntersectionObserver } from '@vueuse/core'export const lazyPlugin = {install(app) {// 定义懒加载指令app.directive('img-lazy', {mounted(el, binding) {// el: 当前DOM元素(img标签)// binding.value: 指令绑定的值(图片URL)const { stop } = useIntersectionObserver(el,([{ isIntersecting }]) => {if (isIntersecting) {// 当图片进入视口时el.src = binding.valueel.classList.add('fade-in') // 添加淡入效果stop() // 停止监听}})}})}
}

在main.js中注册插件

// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import { lazyPlugin } from './directives' // 自动加载index.jscreateApp(App).use(lazyPlugin) // 注册懒加载插件.mount('#app')

创建图片懒加载组件

<!-- src/components/ImageList.vue -->
<template><div class="image-list"><h2>图片懒加载示例</h2><div class="images-container"><!-- 使用v-img-lazy指令 --><img v-for="item in imageList" :key="item.id" v-img-lazy="item.url" alt="懒加载图片"class="lazy-image"/></div></div>
</template><script>
export default {name: 'ImageList',data() {return {imageList: [{ id: 1, url: 'https://picsum.photos/800/600?random=1' },{ id: 2, url: 'https://picsum.photos/800/600?random=2' },{ id: 3, url: 'https://picsum.photos/800/600?random=3' },{ id: 4, url: 'https://picsum.photos/800/600?random=4' },{ id: 5, url: 'https://picsum.photos/800/600?random=5' },{ id: 6, url: 'https://picsum.photos/800/600?random=6' },{ id: 7, url: 'https://picsum.photos/800/600?random=7' },{ id: 8, url: 'https://picsum.photos/800/600?random=8' },{ id: 9, url: 'https://picsum.photos/800/600?random=9' },{ id: 10, url: 'https://picsum.photos/800/600?random=10' }]}}
}
</script><style scoped>
.images-container {display: grid;grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));gap: 20px;padding: 20px;
}.lazy-image {width: 100%;height: 200px;background-color: #f0f0f0;border-radius: 8px;object-fit: cover;opacity: 0;transition: opacity 0.5s ease;
}.fade-in {opacity: 1;
}
</style>

在App.vue中使用组件

<!-- src/App.vue -->
<template><div id="app"><!-- 添加一些内容,使页面可以滚动 --><div style="height: 1500px;"></div><ImageList /></div>
</template><script>
import ImageList from './components/ImageList.vue'export default {name: 'App',components: {ImageList}
}
</script>

运行项目

npm run dev

访问页面并滚动页面查看懒加载效果,可以在F12的网络面板观察img的请求状况。

在这里插入图片描述

http://www.xdnf.cn/news/15911.html

相关文章:

  • k8s的calico无法启动报错解决
  • Docker实践:使用Docker部署blog轻量级博客系统
  • 【Java + Vue 实现图片上传后 导出图片及Excel 并压缩为zip压缩包】
  • 【跨国数仓迁移最佳实践2】MaxCompute SQL执行引擎对复杂类型处理全面重构,保障客户从BigQuery平滑迁移
  • IDEA 同时修改某个区域内所有相同变量名
  • 深入解析IP协议:组成、地址管理与路由选择
  • Freemarker实现下载word可能遇到的问题
  • docker--挂载
  • 深入解析:如何在Kafka中配置Source和Sink连接器构建高效数据管道
  • 【Linux指南】Linux系统 -权限全面解析
  • 万界星空科技锂电池MES解决方案
  • 移星科技 modbus-tcp 转 modbus-Rtu模块
  • CMakeLists.txt 中一些最常见和核心的命令
  • AG32 cpld部分bin的烧录
  • 20250721
  • .NET依赖注入IOC你了解吗?
  • 14.6 《3步实战LLaMA2-7B指令微调:Hugging Face生态+LoRA技术,MT-Bench得分从5.1直升7.3》
  • 基于爬虫技术的电影数据可视化系统 Python+Django+Vue.js
  • C# 集合(Collection)
  • Playwright-MCP浏览器会话复用全解析
  • 企业管理效能提升之道——固定资产管理的价值体现
  • Flutter和Kotlin的对比
  • 北京-4年功能测试2年空窗-报培训班学测开-第五十六天
  • 【Docker#3】Window 和 Linux 上 docker安装 相关知识
  • 算法训练营day25 回溯算法④ 补充联系题目 332.重新安排行程、51. N皇后、37. 解数独
  • 【详细笔记】两类曲线积分转换
  • 14.多播与广播
  • ESMFold 安装教程
  • Linux主机 ->多机器登录
  • 尚庭公寓--------登陆流程介绍以及功能代码