微信小程序顶部tab切换以及滑动

效果图图片如下

 

实现代码xml中

<view class="swiper_tab_view">
 <scroll-view scroll-x='true' scroll-left="{{scrollleft}}" class='tab-h'>
 <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0"
     bindtap="swichNav">要闻</view> 

    <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" 
    bindtap="swichNav">视频</view>

      <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" 
    bindtap="swichNav">推荐</view> 

     <view class="swiper-tab-list {{currentTab==3 ? 'on' : ''}}" data-current="3" 
    bindtap="swichNav">上海</view> 

      <view class="swiper-tab-list {{currentTab==4 ? 'on' : ''}}" data-current="4" 
    bindtap="swichNav">娱乐</view> 

      <view class="swiper-tab-list {{currentTab==5 ? 'on' : ''}}" data-current="5" 
    bindtap="swichNav">体育</view>  
    </scroll-view>
</view>

 <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">  
    
     <swiper-item>  
      <view>要闻</view>  
    </swiper-item>   
     
    <swiper-item>  
      <view>视频</view>  
    </swiper-item>   

        <swiper-item>  
      <view>推荐</view>  
    </swiper-item>

        <swiper-item>  
      <view>上海</view>  
    </swiper-item>

        <swiper-item>  
      <view>娱乐</view>  
    </swiper-item>

        <swiper-item>  
      <view>体育</view>  
    </swiper-item>
    
 </swiper>





js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    scrollleft:0,
    currentTab: 0,  
  },
  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
       currentTab: e.detail.current
        });

      this.checkCor();
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  checkCor:function(){
    if(this.data.currentTab>4){
      this.setData({
        scrollleft:300
      })
    }else{
      this.setData({
        scrollleft:0
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });  
    
  },

wxss

/**index.wxss**/
.tab-h{
  height: 57rpx;
  width: 100%;
  line-height: 51rpx;
  background: white;
  font-size: 16px;
  white-space: nowrap;
  z-index: 999;
  margin-top: 20rpx;
}

.swiper_tab_view{
  width: 100%;
  text-align: center;
  line-height: 80rpx;
}

.swiper-tab-list {
  font-size: 30rpx;
  display: inline-block;
  width: 20%;
  color: #777;
}

.on {
  color: #da7c0c;
  border-bottom: 5rpx solid #da7c0c;
}

.swiper-box {
  display: block;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.swiper-box view {
  text-align: center;
}

这样实现了效果如果顶部tab不是很多的话就可以不实用scroll-view 嵌套在外层

忘了参考谁的代码写的了,实在抱歉, 这里就不给出demo了,复制过去就可以实现效果,

 

微信小程序中的tab切换下划线滑动效果是指在用户切换不同tab(标签)时,底部的指示线会随着当前选中的tab移动,产生滑动效果。这样的效果通常用于给用户提供视觉上的反馈,表明当前所在的页面或选项。 要实现这种效果,可以通过微信小程序提供的组件和API来完成。具体来说,可以通过以下步骤实现: 1. 使用`<view>`标签创建tab栏,并为每个tab设置相应的数据绑定和点击事件处理函数。 2. 使用`wx.createAnimation`创建一个动画实例,并通过动画方法设置下划线的移动效果。 3. 在tab点击事件的回调函数中,根据当前选中的tab位置,更新下划线的样式,使下划线移动到相应的位置。 这里是一个简化的代码示例: ```javascript Page({ data: { activeTab: 0, tabWidth: 250, // 假设每个tab的宽度是250px tabHeight: 40 // 假设下划线的高度是40px }, changeTab(e) { const newActiveTab = e.detail.index; this.setData({ activeTab: newActiveTab }); this.updateUnderline(newActiveTab); }, updateUnderline(index) { const animation = wx.createAnimation({ duration: 300, timingFunction: 'ease', }); const x = index * this.data.tabWidth; animation.move({ x: x, y: 0, duration: 300, timingFunction: 'ease', }).step(); this.setData({ underlineStyle: animation.export() }); } }); ``` 在wxml中定义tab栏和下划线: ```xml <view class="tab-bar"> <block wx:for="{{tabs}}" wx:key="unique"> <view class="tab-item" bindtap="changeTab" data-index="{{index}}" wx:if="{{index === activeTab}}"> {{item.title}} </view> </block> <view class="tab-underline" style="{{underlineStyle}}"></view> </view> ``` 在wxss中设置tab栏和下划线的样式: ```css .tab-bar { display: flex; } .tab-item { height: 40px; line-height: 40px; /* 其他样式 */ } .tab-underline { height: 4px; width: 250px; background-color: #1AAD19; position: absolute; bottom: 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值