21:轮播图

本文详细介绍了如何使用微信小程序开发组件m-swiper,实现轮播图功能。组件包括自动切换、时间间隔设置、指示点等特性,并通过在test.json和test.js中配置数据,成功展示轮播图。组件的样式和属性设置确保了图片的宽度适应及良好的用户体验。
摘要由CSDN通过智能技术生成

components/m-swiper.wxml:

<swiper class="swiper" autoplay indicator-dots  indicator-active-color="#ff5777"  
circular
interval="3000">
  <block wx:for="{{list}}" wx:key="index">
    <swiper-item class="swiper-item">
     <image src="{{item}}" mode="widthFix"/>
    </swiper-item>
  </block>
</swiper>

interval:图片切换的时间
autoplay:自动切换

components/m-swiper.js:

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})

components/m-swiper.json:

{
  "component": true,
  "usingComponents": {}
}

component/m-swiper/m-swiper.wxss:

.swiper{
  height: 300rpx;
}
.swiper-item image{
  width: 100%;
}

把m-swiper做成组件

test.json:

<!--轮播图 m-swiper-->
<m-swiper list="{{banners}}"></m-swiper>

test.js:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners:''
  },


  /**
   * 生命周期函数--监听页面加载 这个onload指的是每次刷新的时候
   * that 指向的success 所有需要 var that = this
   */
  onLoad: function (options) {
    var that = this;
    //轮播图
    const banners=[
      'https://img12.360buyimg.com/da/jfs/t1/64942/6/10328/80741/5d7e487bE321598a3/8f6f08ad6f659916.jpg',
      'https://img11.360buyimg.com/da/jfs/t1/83953/12/10463/95928/5d7e480dE254e4091/03b53610071cbcce.jpg',
      'https://img12.360buyimg.com/da/jfs/t1/69850/2/10309/68489/5d7e4779Ea0afd37c/939073545ae7c55b.jpg',
      'https://img13.360buyimg.com/da/jfs/t1/40938/3/14667/84295/5d79afb6E01c341bb/2ee1208bc35b605d.jpg'
            ]
    //轮播图设置数据
    that.setData({
      banners: banners
    })
  },

test.json:

{
  "usingComponents": {
    "m-swiper":"/pages/component/m-swiper/m-swiper"
  },
}

这里components/m-swiper.js 中的list要和test.json中list属性名要一致

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值