图片预览 element-plus 带页码

vue3、element-plus项目中,点击预览图片,并显示页码效果如图

安装 | Element Plus

html 复制代码
  <div class="image__preview">
    <el-image
      style="width: 100px; height: 100px"
      :src="imgListArr[0]"
      :zoom-rate="1.2"
      :max-scale="7"
      :min-scale="0.2"
      :preview-src-list="imgListArr"
      :initial-index="0"
      fit="cover"
      @switch="onSwitch"
      @close="closePreview"
    >
      <template #viewer >
        <div class="preview-page" >{{imgCur}}/{{imgListArr.length}}</div>
      </template>
    </el-image>
  </div>
javascript 复制代码
<script setup>
//预览图片的数组
const imgListArr = ref([
  'https://img1.baidu.com/it/u=1461245034,737626739&fm=253&fmt=auto&app=138&f=JPEG?w=751&h=500',
  'https://img0.baidu.com/it/u=1272888261,2716721561&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
  'https://img1.baidu.com/it/u=3390302890,2011160393&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500',
  'https://img0.baidu.com/it/u=3944258063,3004524010&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800',
])

//页码默认1开始
const imgCur = ref(1)

//监听当前切换的图片
const onSwitch = (index) =>{
  imgCur.value = index+1
}
//监听预览的关闭
const closePreview = () =>{
  //关闭时需要把页码重置1,要不然切换到比如5、6时关闭预览,下次打开就是5、6
  imgCur.value = 1
}

</script>
css 复制代码
<style scoped lang="less">
.image__preview{
  float: left;
  .el-image {
    border:1px solid rgb(204 204 204 / .5);
  }
  .preview-page{
    position: absolute;
    background:rgb(0 0 0 / .5);
    text-align: center;
    color:#fff;
    left: 50%;
    bottom:80px;
    transform: translateX(-50%);
    width:50px;
    height:30px;
    padding: 0 23px;
    border-radius:15px;
    line-height:30px;
  }
}
.image__error{
  display: inline-block;
  .image-slot {
    font-size: 30px;
    .el-icon {
      font-size: 30px;
    }
  }
  .el-image {
    width: 100%;
    height: 200px;
  }
}
</style>

src 默认展示的图片地址

initial-index 默认点开查看的第几个大图的索引

preview-src-list 要预览的图片数组

switch 监听切换图片方法的回调,放回当前预览的图片索引

close 监听关闭预览图片事件

viewer 是官网提供的插槽,官网提供的预览没有页码,可以通过插槽和switch回调来写页码

预览图片官网API:Image 图片 | Element Plus

相关推荐
云水一下2 小时前
从零开始!VMware安装Fedora Workstation 44桌面系统完整教程
前端
小码哥_常3 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
前端
killerbasd4 小时前
还是迷茫 5.3
前端·react.js·前端框架
不会敲代码14 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen5 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦5 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen5 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
不会敲代码15 小时前
从零搭建 AI 日记助手:用 Milvus 向量数据库实现语义搜索
javascript·openai
千寻girling5 小时前
《 Git 详细教程 》
前端·后端·面试
threelab6 小时前
Three.js UV 图像变换效果 | 三维可视化 / AI 提示词
javascript·人工智能·uv