工作生活

小程序导航tab滑动切换效果

2019-07-03  本文已影响0人  不忘初心_d

欢迎加我技术交流QQ群 :811956471

<view class="tba_contain">
  <!-- 导航栏 -->
  <scroll-view class="tab" scroll-x scroll-left="{{tabScroll}}" scroll-with-animation="true">
    <block wx:for="{{menuList}}" wx:key="index">
      <view class="tab-item {{currentTab == index ? 'active' : ''}}" data-current="{{index}}" bindtap='clickMenu'>{{item.name}}</view>
    </block>
  </scroll-view>
  <!-- 页面 -->
  <swiper class="list_content" style='height: {{height}}px' duration="500" current="{{currentTab}}" bindchange="changeContent">
    <swiper-item class="content-item" wx:for="{{menuList}}" wx:key="index">这里是{{item.name}}</swiper-item>
  </swiper>
</view>
.tba_contain {
  width: 100%;
  height: 100%;
}

.tab {
  width: 100%;
  height: 100rpx;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  white-space: nowrap;
  box-sizing: border-box;
  overflow: hidden;
  line-height: 100rpx;
}

.tab-item {
  display: inline-block;
  width: 20%;
  text-align: center;
  font-size: 14px;
  color: #8f9193;
}

.active {
  color: #ff502c;
}

.list_content {
  padding-top: 100rpx;
  box-sizing: border-box;
  text-align: center;
  font-size: 14px;
  min-height: 100vh;
}

.content-item {
  overflow-y: scroll;
}

Page({
  data: {
    menuList: [{
      name: "快车"
    }, {
      name: "顺风车"
    }, {
      name: "外卖"
    }, {
      name: "单车"
    }, {
      name: "礼橙专车"
    }, {
      name: "出租车"
    }, {
      name: "公交"
    }, {
      name: "代驾"
    }, {
      name: "豪华车"
    }, {
      name: "自驾租车"
    }, {
      name: "拼车"
    }, {
      name: "二手车"
    }],
    tabScroll: 0,
    currentTab: 0,
   
  },
  onLoad: function() {
    wx.getSystemInfo({ // 获取当前设备的宽高,文档有
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
  },
  onReady: function() {
   
  },
  //点击tab menu
  clickMenu: function(e) {
    var current = e.currentTarget.dataset.current; //获取当前tab的index
    // 导航tab共5个,获取一个的宽度
    var tabWidth = this.data.windowWidth / 5;
    this.setData({
      tabScroll: (current - 2) * tabWidth//使点击的tab始终在居中位置
    })
    if (this.data.currentTab == current) {
      return false
    } else {
      this.setData({
        currentTab: current
      })
    }
  },
  //活动menu 内容
  changeContent: function(e) {
    var current = e.detail.current // 获取当前内容所在index,文档有
    var tabWidth = this.data.windowWidth / 5
    this.setData({
      currentTab: current,
      tabScroll: (current - 2) * tabWidth
    })
  }
})
上一篇下一篇

猜你喜欢

热点阅读