我们都知道点击tab按钮的时候,可以让scroll-view滑到指定的地方(scroll-into-view),但是手动滑到某个地方的时候,却不能反过来控制选中对应的tab,这时就只能自定义了
<scroll-view @scroll="scrolling" scroll-y="true" :scroll-with-animation="true" :scroll-into-view="valuescrollto"
:style="'height:'+cardscrollheight+'px;'">
</scroll-view>
scrolling(e) {
if (!this.heightgot) {//在onshow或者mounted中获取不到滚动元素的高度或者获取的高度都是0,所以只能在这里获取,并且控制到只获取一次
let query = uni.createSelectorQuery().in(this)
let _this = this
query.select('#itemindex0').boundingClientRect(data => {
_this.onetotop = data.top
}).exec();
query.select('#itemindex1').boundingClientRect(data => {
_this.twototop = data.top
}).exec();
query.select('#itemindex2').boundingClientRect(data => {
_this.threetotop = data.top
}).exec();
query.select('#itemindex3').boundingClientRect(data => {
_this.fourtotop = data.top
}).exec();
this.heightgot = true//保证只获取一次
} else {
if (this.aftertab) {//如果是点击了tab而产生的滚动,就不作处理
this.aftertab = false
return
}
if (e.detail.scrollTop < this.twototop - this.onetotop) {//手指滑动
this.tabIndex = 0
} else if (e.detail.scrollTop < this.threetotop - this.onetotop) {
this.tabIndex = 1
} else if (e.detail.scrollTop < this.fourtotop - this.onetotop) {
this.tabIndex = 2
} else {
this.tabIndex = 3
}
this.valuescrollto = ''
}
},