开发小程序轮播图组件-简易

关于小程序轮播图组件

组件开发

在components中创建一个目录customSwiper在此目录下创建一个组件
wxml

<!-- 轮播图组件  -->
<view class='swiper'>
  <swiper  autoplay="true" interval="5000" duration="1000" style="height:{{Height}};width:{{Width}};">
    <block wx:for="{{imgUrls}}" wx:key="*this">
      <swiper-item>
        <image src="{{item}}" class="slide-image" mode="aspectFill" />
      </swiper-item>
    </block>
  </swiper>

  <button bindtap='m'></button>
</view>

json

{
  "component": true
}

js

Component({
  // 私有数据
  data: {

  },

  // 方法
  methods: {
    m() {

      // console.log('触发了!');
    }
  },

  // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  lifetimes: {
    attached: function() {
      console.log('attached');
    },
    moved: function() {},
    detached: function() {},
  },

  // 组件所在页面的生命周期函数
  pageLifetimes: {
    show: function() {
      console.log('生命show!');
    },
  },

  // 变量替换以及修改
  properties: {
  //轮播图图片参数
    imgUrls: {
      type: Array,
      value: [],
      observer: function(newVal, oldVal) {
        this.setData({
          imgList: newVal
        })
      }
    },  
    //轮播图高度参数
    Height: String,
    //轮播图宽度参数
    Width:String
  }
})

页面使用

在页面json中

{
  "usingComponents": {
    "custom-swiper": "../../components/customSwiper/customSwiper"
  }
}

在页面中

 <!-- 轮播图 -->
  <!-- imgUrls, Height,Width都是组件中prop的参数-->
  <custom-swiper imgUrls="{{自己获取的轮播图数据}}" Height="400rpx" Width="700rpx" />
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值