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,
  }
}
相关推荐
( ̄(工) ̄)霸天下20 分钟前
Typescript速通教程
前端·javascript·typescript
妮妮喔妮1 小时前
图片上传 el+node后端+数据库
javascript·数据库·vue.js
paopaokaka_luck5 小时前
婚纱摄影管理系统(发送邮箱、腾讯地图API、物流API、webSocket实时聊天、协同过滤算法、Echarts图形化分析)
vue.js·spring boot·后端·websocket·算法·echarts
一只小风华~5 小时前
JavaScript 函数
开发语言·前端·javascript·ecmascript·web
仰望星空的凡人7 小时前
【JS逆向基础】数据库之MongoDB
javascript·数据库·python·mongodb
若梦plus8 小时前
Nuxt.js基础与进阶
前端·vue.js
樱花开了几轉8 小时前
React中为甚么强调props的不可变性
前端·javascript·react.js
若梦plus8 小时前
Vue服务端渲染
前端·vue.js
Mr...Gan9 小时前
VUE3(四)、组件通信
前端·javascript·vue.js
给力学长9 小时前
自习室预约小程序的设计与实现
java·数据库·vue.js·elementui·小程序·uni-app·node.js