微信小程序UI之旅:自定义轮播图组件

轮播图展示

image.png

github传送门:https://github.com/albert-lii/wx-abui/tree/master/abui/widgets/ab-banner
demo传送门:https://github.com/albert-lii/wx-abui/tree/master/pages/mainex

自定义属性和方法

属性描述
src数据源
current-title当前页面标题
interval页面切换间隔时间
duration页面滑动动画时长
placeholder图片未加载完成时的占位图
error图片加载失败时的占位图
方法描述
bindchange页面切换监听
binditemtap页面点击监听

使用示例

<view class="container">
     <ab-banner class="banner" src="{{bannerList}}" current-title="{{bannerTitle}}" bindchange="bannerChanged" binditemtap="bannerItemTap" />
</view>
.banner {
  width: 100%;
  height: 362rpx;
}
Page({

  data: {
    bannerList: [{
      title: '测试1',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/469/picture4.jpg'
    },
    {
      title: '测试2',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/4626/25.jpg'
    },
    {
      title: '测试3',
      imgUrl: 'https://shenpan.oss-cn-shanghai.aliyuncs.com/469/picture4.jpg'
    }
    ],
    bannerTitle: '测试1'
  },

  /**
   * banner 页面切换监听
   */
  bannerChanged: function (e) {
    let _current = e.detail.current;
    this.setData({
      bannerTitle: this.data.bannerList[_current].title
    });
  },
  /**
   * banner item 点击监听
   */
  bannerItemTap: function (e) {
    wx.showToast({
      icon: 'none',
      title: '点击了第' + e.detail.dataset.index + '个页面',
      duration: 1000
    })
  }
});
{
  "navigationBarTitleText": "abui",
  "usingComponents": {
    "ab-banner": "../../abui/widgets/ab-banner/ab-banner"
  }
}

源码

注:轮播图组件中引用了ab-easy-image组件,使用时请至 wx-abui 项目中将ab-easy-image一起拷贝下来,或者直接将ab-easy-image改为普通的image组件,则placeholdererror属性不可用。

  • ab-banner.wxml
<view class="banner">
  <swiper class="banner-slider" indicator-dots="true" indicator-color="rgba(255, 255, 255, .3)" indicator-active-color="rgba(255, 255, 255,.8)" autoplay="true" circular="true" interval="{{interval}}" duration="{{duration}}" bindchange="_itemChanged">
    <swiper-item wx:for="{{src}}" wx:for-index="index" wx:for-item="item" wx:key="{{index}}" bindtap="_itemTap" data-index="{{index}}" data-item="{{item}}">
      <ab-easy-image class="banner-item-image" src="{{item.imgUrl}}" placeholder="{{placeholder}}" error="{{error}}" mode="aspectFill" />
    </swiper-item>
  </swiper>
  <text class="banner-item-title" wx:if="{{currentTitle}}">{{currentTitle}}</text>
</view>
  • ab-banner.wxss
.banner {
  position: relative;
  width: 100%;
  height: 100%;
}

.banner-slider {
  width: 100%;
  height: 100%;
}

.banner-item-image {
  width: 100%;
  height: 100%;
}

.banner-item-title {
  position: absolute;
  font-size: 20rpx;
  color: white;
  line-height: 20rpx;
  right: 20rpx;
  bottom: 20rpx;
}

  • ab-banner.js
Component({
  properties: {
    // banner 数据源
    src: {
      type: Array,
      value: null
    },
    // 当前页面的标题
    currentTitle: {
      type: String,
      value: ''
    },
    // 页面切换间隔时间
    interval: {
      type: Number,
      value: 5000
    },
    // 页面滑动动画时长
    duration: {
      type: Number,
      value: 500
    },
    // 图片未加载完成时的占位图
    placeholder: {
      type: String,
      value: ''
    },
    // 图片加载失败时的占位图
    error: {
      type: String,
      value: ''
    },
  },

  methods: {
    /**
     * 页面切换监听
     */
    _itemChanged: function(e) {
      let _current = e.detail.current;
      this.triggerEvent('change', {
        current: _current
      });
    },
    /**
     * banner item 点击事件
     */
    _itemTap: function(e) {
      console.log(e)
      let _currentTarget = e.currentTarget;
      this.triggerEvent('itemtap', _currentTarget);
    }
  }
})
  • ab-banner.json
{
  "component": true,
  "usingComponents": {
    "ab-easy-image": "../ab-easy-image/ab-easy-image"
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值