微信小程序scroll-view组件实现点击tab横向滚动动画

1.效果图

在这里插入图片描述

2.思路

这个效果其实可以使用swiper组件实现,但是swiper组件必须设置一个固定的高度。

因此这里使用scroll-view组件中的属性scroll-x声明滑动方向为横向,属性scroll-into-view为自动滚动到携带目标id的某个元素,属性scroll-with-animation设置滚动动画过渡。如图所示:

在这里插入图片描述

2.wxml部分

代码如下(示例):
<view class="swiper_tab">
	<view class="tab_list {{currentTab == 0 ? 'on' : ''}}" data-current="0" bindtap="scrollToView">获奖情况</view>
	<view class="tab_list {{currentTab == 1 ? 'on' : ''}}" data-current="1" bindtap="scrollToView">处分情况</view>
</view>
<scroll-view class="scroll_box" scroll-x="true" scroll-into-view="{{scrollView}}" scroll-with-animation="true">
	<!-- 注意这里的id -->
	<view class="scroll_item" id="toView0" catchtouchmove='catchTouchMove'>
		<table headers="{{awardTableHeader}}" data="{{awardTableData}}" border="{{true}}" />
   	</view>
    <view class="scroll_item" id="toView1" catchtouchmove='catchTouchMove'>
    	<table headers="{{outoTableHeader}}" data="{{outoTableData}}" border="{{true}}" />
    </view>
</scroll-view>

3.wxss部分

代码如下(示例):
.swiper_tab { 
	width: 100%; 
	margin-bottom: 15rpx;
}
.swiper_tab .tab_list { 
	font-size: 26rpx; 
	display: inline-block; 
	color: #656766; 
	margin-right: 35rpx;
}
.swiper_tab .on { 
	color: #282828; 
	font-size: 29rpx; 
	font-weight: bold;
	 position: relative;
}
.swiper_tab .on::after { 
	position: absolute; 
	content: ''; 
	display: inline-block; 
	width: 80%; 
	height: 8rpx; 
	bottom: -10rpx; 
	left: 50%; 
	transform: translateX(-50%); 
	border-radius: 5rpx; 
	background: #03a57e;
}
.scroll_box { 
	white-space: nowrap;
}
.scroll_item { 
	width: 100%; 
	display: inline-block;
}

4.js部分

Page({
  data: {
    currentTab: 0,
    scrollView: ''
  },
  //  tab切换
  scrollToView: function (e) {
    var i = e.target.dataset.current
    if(this.data.currentTab === i) {
      return false
    } else {
      this.setData({ currentTab: i })
    }
    this.setData({ scrollView: 'toView' + i })
  },
  // 取消scroll-view组件手动滑动
  catchTouchMove: function (e) {
    return false
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值