需求: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>
相关推荐
天天鸭11 分钟前
前端仔写了个 AI Agent,才发现大模型只干了 10% 的活
前端·python·ai编程
发现一只大呆瓜31 分钟前
前端模块化:CommonJS、AMD、ES Module三大规范全解析
前端·面试·vite
IT_陈寒34 分钟前
一文搞懂JavaScript的核心概念
前端·人工智能·后端
IT_陈寒36 分钟前
Java开发者必看!5个提升开发效率的隐藏技巧,你用过几个?
前端·人工智能·后端
前端Hardy40 分钟前
Wails v3 正式发布:用 Go 写桌面应用,体积仅 12MB,性能飙升 40%!
前端·javascript·go
Laurence1 小时前
Qt 前后端通信(QWebChannel Js / C++ 互操作):原理、示例、步骤解说
前端·javascript·c++·后端·交互·qwebchannel·互操作
Pu_Nine_91 小时前
JavaScript 字符串与数组核心方法详解
前端·javascript·ecmascript
码云数智-园园1 小时前
从输入 URL 到页面展示:一场精密的互联网交响乐
前端
2501_915918411 小时前
苹果App Store上架审核卡住原因分析与解决方案指南
android·ios·小程序·https·uni-app·iphone·webview
秋水无痕1 小时前
# 手把手教你从零搭建 AI 对话系统 - React + Spring Boot 实战(一)
前端·后端