uniapp实战 —— 轮播图【数字下标】(含组件封装,点击图片放大全屏预览)

组件封装

src\components\SUI_Swiper2.vue

html 复制代码
<script setup lang="ts">
import { ref } from 'vue'
const props = defineProps({
  config: Object,
})

const activeIndex = ref(0)
const change: UniHelper.SwiperOnChange = (e) => {
  activeIndex.value = e.detail.current
}

// 点击图片时
const onTapImage = (url: string) => {
  // 大图预览
  uni.previewImage({
    current: url,
    urls: props.config?.itemList,
  })
}
</script>

<template>
  <view class="preview">
    <swiper
      @change="change"
      :circular="props.config?.circular || true"
      :autoplay="props.config?.autoplay || false"
      :interval="props.config?.interval || 3000"
    >
      <swiper-item v-for="(item, index) in props.config?.itemList" :key="index">
        <image @tap="onTapImage(item)" mode="aspectFill" :src="item" />
      </swiper-item>
    </swiper>
    <view class="indicator">
      <text class="current">{{ activeIndex + 1 }}</text>
      <text class="split">/</text>
      <text class="total">{{ props.config?.itemList.length }}</text>
    </view>
  </view>
</template>

<style lang="scss">
.preview {
  height: 750rpx;
  position: relative;
  .image {
    width: 750rpx;
    height: 750rpx;
  }
  .indicator {
    height: 40rpx;
    padding: 0 24rpx;
    line-height: 40rpx;
    border-radius: 30rpx;
    color: #fff;
    font-family: Arial, Helvetica, sans-serif;
    background-color: rgba(0, 0, 0, 0.3);
    position: absolute;
    bottom: 30rpx;
    right: 30rpx;
    .current {
      font-size: 26rpx;
    }
    .split {
      font-size: 24rpx;
      margin: 0 1rpx 0 2rpx;
    }
    .total {
      font-size: 24rpx;
    }
  }
}
</style>

页面使用

src\pages\goods\goods.vue

html 复制代码
<SUI_Swiper2 :config="swiperConfig" />
js 复制代码
let swiperConfig = ref({ itemList: [] })

// 获取商品详情信息
const goods = ref<GoodsResult>()
const getGoodsByIdData = async () => {
  const res = await getGoodsByIdAPI(query.id)
  goods.value = res.result
  swiperConfig.value = {
    itemList: goods?.value.mainPictures,
  }
}
相关推荐
酒尘&21 分钟前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
用户47949283569152 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569152 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
JIngJaneIL2 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
+VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue心理健康管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
大怪v3 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
蓝瑟6 小时前
告别重复造轮子!业务组件多场景复用实战指南
前端·javascript·设计模式
老华带你飞6 小时前
旅游|基于Java旅游信息系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·旅游
韭菜炒大葱6 小时前
别等了!用 Vue 3 让 AI 边想边说,字字蹦到你脸上
前端·vue.js·aigc
渴望成为python大神的前端小菜鸟7 小时前
浏览器及其他 面试题
前端·javascript·ajax·面试题·浏览器