uniapp中swiper一屏展示两张图的轮播

 代码如下:

<template>

<view class="container">
  <swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
  :duration="duration">
    <swiper-item class="swiper-group">
      <view class="swiper-item uni-bg-red">A</view>
      <view class="swiper-item uni-bg-red">A2</view>
    </swiper-item>
    <swiper-item class="swiper-group">
      <view class="swiper-item uni-bg-green">B</view>
      <view class="swiper-item uni-bg-green">B2</view>
    </swiper-item>
    <swiper-item class="swiper-group">
      <view class="swiper-item uni-bg-blue">C</view>
      <view class="swiper-item uni-bg-blue">C2</view>
    </swiper-item>
  </swiper>
</view>

////////////////////////////////////////////////////////

请求接口时:这样写

    <view class="container">
        <swiper class="swiper" :interval="3000" :circular="true" autoplay>
            <block v-for="(group, groupIndex) in groupList" :key="groupIndex">
                <swiper-item class="swiper-group">
                    <view v-for="(item, itemIndex) in group" :key="itemIndex" class="item">{{ item }}</view>
                </swiper-item>
            </block>
        </swiper>
    </view>
///////////////////////////////////////////////////////////// </template> <script> export default { data() { return { groupList: [ ['Image 1', 'Image 2', 'Image 3'], ['Image 4', 'Image 5', 'Image 6'], ['Image 7', 'Image 8', 'Image 9'] ], }; }, onLoad() { this.startAutoScroll(); }, methods: { startAutoScroll() { setInterval(() => { const swiper = uni.createSelectorQuery().select('.swiper'); swiper.boundingClientRect((rect) => { if (rect) { const scrollLeft = rect.scrollLeft + rect.width; swiper.scrollBy({ left: scrollLeft, behavior: 'smooth', }); } }).exec(); }, 3000); }, }, }; </script> <style> .container { height: 200px; overflow: hidden; } .swiper { white-space: nowrap; height: 100%; } .swiper-group { display: inline-block; width: 100%; white-space: initial;
     display: flex;
     justify-content: space-between;
} .item { height:
100%; width: 48%; display: inline-block; line-height: 200px; text-align: center; background-color: #ccc; } </style>

 

posted @ 2024-01-24 16:01  IT小姐姐  阅读(111)  评论(0编辑  收藏  举报