微信小程序横向滚动条自定义组件

  1. 效果

  1. 组件代码

scroll-tab.wxml

<!--/component/scroll-tab/scroll-tab.wxml-->
<view class="contain-bar">
  <scroll-view scroll-x scroll-into-view="{{itemid}}" scroll-with-animation='true'  class="scroll-tab" id='scrollTab' >
    <view class="scroll-list" animation="{{animation}}">
        <view class="scroll-item" wx:for="{{data}}" wx:key="index"  
        bindtap="translate"  id="item{{index}}"
        data-index="{{index}}"
        data-key="{{item.key}}"
        data-msg="{{item}}">
          <text style="color:{{activeKey==item.key?activeColor:textColor}}; border-bottom:{{activeKey==item.key?'4rpx solid #ed4861':none}};font-size:26rpx;padding:6rpx 0;">{{item.vulue}}</text>   
        </view>
    </view>
  </scroll-view>
</view>

scroll-tab.js

//component/scroll-tab/scroll-tab.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: { //要渲染的列表
      type: Array,
      value: [],
    },
    activeKey: { //选中的key
      type: Number,
      value: '',
    },
    activeColor: { //选中的颜色
      type: String,
      value: '#ed4861'
    },
    textColor: {
      type: String,
      value: '#666'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tableft:0,
    itemid:null
  },

  lifetimes: {
    attached: function () {
      this.animation = wx.createAnimation()
      this.tabLeftPostion=0
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    translate (e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
        activeKey:e.currentTarget.dataset.key,
        itemid:'item'+(index-2)
      }) 
      var myEventDetail = {data:e.currentTarget.dataset.msg} // detail对象,提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('myevent', myEventDetail, myEventOption)
    },
  }

})

scroll-tab.wxss

/* moduleIndependent/component/scroll-tab/scroll-tab.wxss */
.contain-bar{
  height: 98rpx;
  width: 100%;
  overflow-y: hidden;
}
.scroll-tab{
  width: 100%;
  height: 110rpx;
  line-height: 98rpx;
  overflow-y: hidden;
  overflow-x: scroll;
  z-index: 99;  
  position: relative;
}
.scroll-list{
  word-wrap: normal;
  white-space: nowrap;
  width: 100%;
  background: #fff;
  display: inline-block;
  position: absolute;
}
.scroll-item{
   padding:0 20rpx;
   position: relative;
   display: inline-block;
   color: #666;
}

scroll-tab.json

{
  "component": true,
  "usingComponents": {}
}
  1. 使用

在要使用的页面引入组件

在wsml中使用组件。

data(tabList)为对象数组,对象中有2个属性key和value,展示的是value,key用于控制选中激活,activeKey为当前选中,bindmyevent为滚动选中事件。

  //滚动选中事件
  seleteClass(e) {
    this.setData({
      // 清空页面展示数组,下次查询第一页
      pageNum: 1,
      informationList: [],
      classCode: e.detail.data.key, // 选中值
      isLoad: false, //是否加载完成
      isShowLoad: true, //是否显示底部加载条
    })
    // 刷新页面展示
    this.getInformationList();
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值