需求:h5和小程序预览图片需要有当前第几张标识

1.小程序直接使用api:uni.previewImage

2.h5使用轮播图写一个组件

javascript 复制代码
<template>
    <view class="custom-image-preview" v-if="visible">
      <view class="overlay"></view>
      <swiper class="swiper" :current="currentIndex" @change="onSwiperChange">
        <swiper-item v-for="(image, index) in images" :key="index" @click="closePreview">
          <image style="width: 100%;height: 100%;" :src="image" mode="aspectFit" class="preview-image" :show-menu-by-longpress="true"></image>
        </swiper-item>
      </swiper>
      <view class="index-indicator">
        {{ currentIndex + 1 }} / {{ images.length }}
      </view>
    </view>
  </template>
  
  <script>
  export default {
    props: {
      images: {
        type: Array,
        required: true,
      },
      current: {
        type: Number,
        default: 0,
      },
    },
    data() {
      return {
        visible: false,
        currentIndex: 0,
      };
    },
    methods: {
      openPreview() {
        this.currentIndex = this.current;
        this.visible = true;
      },
      closePreview() {
        this.visible = false;
        this.$emit('close');
      },
      onSwiperChange(e) {
        this.currentIndex = e.detail.current;
      },
    },
    mounted() {
      this.openPreview();
    },
  };
  </script>
  
  <style scoped>
  .custom-image-preview {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
  }
  
  .overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
  }
  
  .swiper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  
  .preview-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  
  .index-indicator {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: #fff;
    font-size: 24px;
    background: rgba(0, 0, 0, 0.5);
    padding: 8px 16px;
    border-radius: 16px;
  }
  </style>
相关推荐
雪碧聊技术6 分钟前
前端VUE3项目部署到linux服务器(CentOS 7)
前端·linux部署vue3项目
酒尘&6 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要6 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569158 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569158 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v9 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式9 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw510 小时前
npm几个实用命令
前端·npm
!win !10 小时前
npm几个实用命令
前端·npm
代码狂想家10 小时前
使用openEuler从零构建用户管理系统Web应用平台
前端