小程序点击tab导航栏滑动居中

图片

在这里插入图片描述

wxml:

<scroll-view class="scroll-view_H"
   scroll-left="{{scroll_left}}"
   scroll-with-animation="true"
   scroll-x="true">
    <view class="scroll-view-item_H">
      <view wx:for="{{topArray}}"
      bindtap="clickTitle"
      data-type = "{{item.type}}"
       class="{{type === item.type ?'active':''}}"
        wx:key="index">{{item.title}}</view>
    </view>
  </scroll-view>

js

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    topArray: [
      { title: '家具', type: 2 },
      { title: '生鲜冻品', type: 13 },
      { title: '美妆护肤', type: 11 },
      { title: '数码电器', type: 12 },
      { title: '食品粮油', type: 14 },
      { title: '母婴用品', type: 15 },
    ],
    type: null, // 默认是类型2
    scroll_left: 0, // 设置滚动条距离左边的位置。
  },
  // 点击头部title的处理事件
  clickTitle(e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    var type = e.currentTarget.dataset.type
    that.setData({
      type: type
    })
    that.getMarkerList(type)
  },
   /**
    * 页面传来的type。
    */
  getMarkerList(page_tab_type) {
    var that = this
    var type = page_tab_type
    that.Scroll_locatation(type)
  },

  Scroll_locatation(type) {
    var that = this
    var item_index = that.whitch_tab_item(type)
    var scroll = 100 * item_index-100
    that.setData({
      type: type,
      scroll_left: scroll
    })
 
  },
  // 判断是那个tab_item
  whitch_tab_item(type) {
    var that = this
    var tab_list = that.data.topArray
    var item_index = null
    tab_list.forEach((item, index) => {
      if (item.type === type) {
        item_index = index
      }
    })
    return item_index
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var tab_list = that.data.topArray
    var id_index = null
    tab_list.forEach((item,index) =>{
      if(item.title == "数码电器"){
        id_index = item.type
      }
    })
    console.log(id_index,"------")

    this.getMarkerList(id_index)
  },
})

css


.scroll-view_H{
  white-space: nowrap;
  margin-bottom:16rpx;
}
.scroll-view-item_H{
  display: inline-block;
  width:70%;
}
.scroll-view-item_H>view{
  display: inline-block;
  width: 100px;
  text-align: center;
  height:67rpx;
  line-height: 67rpx;
}
.scroll-view-item_H>view.active{
  background: #FF6551;
  border-radius: 50px;
  color:#FFF;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值