1. 程式人生 > 實用技巧 >uni-app 實現輪播圖 【數字膠囊】

uni-app 實現輪播圖 【數字膠囊】

官網:https://uniapp.dcloud.io/component/swiper

效果圖:

<view class="swiper">
    <swiper :interval="3000" :duration="1000" :indicator-dots="false"
     :current="topSwiperIndex" @change="topSwiperTab">
        <swiper-item v-for="(item,index) in topSwiper" :key="index">
            <view class
="swiper-item"> <image :src="item.src" mode="aspectFill"></image> </view> </swiper-item> </swiper> <!-- 自定義指示點dots --> <view class="dots"> <text>{{topSwiperIndex+1}}/{{topSwiper.length}}</text>
</view> </view>

data() {
    return {
        topSwiperIndex: 0,
        topSwiper: [
            {
                src: require('../../static/images/1.jpg')
            },
            {
                src: require('../../static/images/1.jpg')
            },
            {
                src: require(
'../../static/images/1.jpg') }, { src: require('../../static/images/1.jpg') }, { src: require('../../static/images/1.jpg') } ] }; },

methods:{
    topSwiperTab(e) {
        var that = this;
        this.topSwiperIndex = Number(e.target.current);
    }
}

注:swiper輪播圖的預設高度是150px,通常我們的輪播圖片高度不是這個。

解決辦法:

指定swiper標籤的寬高,然後在指定image圖片的寬高

swiper{
    width: 100%;
    height: 777.17rpx;
    
    swiper-item{

        image {
            width: 100%;
            height: 781.7rpx;
            display: block;
        }
    }
}

拓展:uni-app實現輪播圖自定義小圓點