美文网首页
小程序tab栏点击自动居中效果

小程序tab栏点击自动居中效果

作者: waiterYu | 来源:发表于2020-08-12 22:03 被阅读0次
image.png

点击自动居中显示

wxml

    <scroll-view class="scroll-view-time" scroll-x="true" scroll-left='{{ toIndex }}' scroll-with-animation="true" bindscroll="scroll" style="width: 100%">
      <block wx:for='{{ dateData }}'>
        <view
          class="item {{ item.status === 2 ? 'item-valid':'' }} {{ index===active.index?'item-checked':'' }}"
          data-index='{{ index }}'
          data-shipDate='{{ item.shipDate }}'
          bind:tap='handleChecked'
        >
          <view class="item-date" hover-class="none" hover-stop-propagation="false">
            {{ item.shipDate }}
          </view>
          <view class="item-state" hover-class="none" hover-stop-propagation="false">
            {{ item.status ===1 ?'已出货':'出货' }}
          </view>
        </view>
      </block>
    </scroll-view>
data:{
     toIndex: 0,
},
  handleChecked(e) {
    const onheadactive = e.currentTarget.offsetLeft // 元素距离左侧的位置
    const winweight = wx.getSystemInfoSync().windowWidth // 设备宽度
    const query = wx.createSelectorQuery()
    query.select('.item').boundingClientRect((rect: any) => {
      this.setData({
        toIndex: onheadactive - winweight / 2 + (rect.width / 2)
      })
    }).exec()
  },

相关文章

网友评论

      本文标题:小程序tab栏点击自动居中效果

      本文链接:https://www.haomeiwen.com/subject/ysqvdktx.html