微信小程序—轮播图+背景变化

这篇博客展示了如何在微信小程序中创建一个带有渐变背景的轮播图组件。通过结合HTML、JS和CSS代码,实现了轮播图图片切换时背景颜色随之变化的效果,轮播图自动播放并具有指示点。关键技术和样式包括linear-gradient渐变色、swiper组件的使用以及事件监听更新索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上效果:

1.轮播图效果

2.背景颜色会随着轮播图变化

代码实现:

HTML:

<view class="tab-list-movie" style="background:{{bgColor[swiperIndex]}}">
    <swiper class="movie-swiper" autoplay interval="3000" circular indicator-dots indicator-active-color="white" bindchange="getSwiperIndex">
        <swiper-item class="swiper-item" wx:for="{{swiperImg}}" wx:key="index">
            <image class="swiper-item-img" mode="heightFix" src="{{item}}" />
        </swiper-item>
    </swiper>
</view>

 JS:

data:{
    //轮播图素材
    swiperImg: [
        "../images/swiper1.jpg",
        "../images/swiper2.jpg",
        "../images/swiper3.jpg",
        "../images/swiper4.jpg"
    ],
    swiperIndex:0,//轮播图索引
    bgColor:[
        "linear-gradient(#33299c,#f1f1f1,#f1f1f1,#f1f1f1)",
        "linear-gradient(#433c36,#f1f1f1,#f1f1f1,#f1f1f1)",
        "linear-gradient(#900b08,#f1f1f1,#f1f1f1,#f1f1f1)",
        "linear-gradient(#eda23c,#f1f1f1,#f1f1f1,#f1f1f1)",
    ],// linear-gradient 渐变色需要四个颜色属性
}

//监听轮播图索引事件
getSwiperIndex(e){
    let currentIndex = e.detail.current
    this.setData({
        swiperIndex:currentIndex
    })
},
.tab-list-movie {
    height: 100vh;
}
.movie-swiper{
    padding-top: 80rpx;
    margin: 0 32rpx;
    height: 200rpx;
}
.swiper-item{
    width: 100%;
    height: 100%;
    border-radius: 16rpx;
}
.swiper-item-img{
    width: 100%;
    height: 100%;
    border-radius: 20rpx;
}

代码展示效果: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值