mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-24 10:22:36 +08:00
[bugfix] Tabs:回滚使用IntersectionObserver实现的sticky效果
This commit is contained in:
parent
7fc0eb7a00
commit
03fa90dd9d
@ -25,5 +25,11 @@ Page({
|
|||||||
title: `点击标签 ${event.detail.index + 1}`,
|
title: `点击标签 ${event.detail.index + 1}`,
|
||||||
icon: 'none'
|
icon: 'none'
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onPageScroll(event) {
|
||||||
|
this.setData({
|
||||||
|
scrollTop: event.scrollTop
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -87,7 +87,7 @@
|
|||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
<demo-block title="粘性布局">
|
<demo-block title="粘性布局">
|
||||||
<van-tabs sticky>
|
<van-tabs sticky scroll-top="{{ scrollTop }}">
|
||||||
<van-tab
|
<van-tab
|
||||||
wx:for="1234"
|
wx:for="1234"
|
||||||
wx:key="index"
|
wx:key="index"
|
||||||
@ -107,7 +107,7 @@
|
|||||||
wx:key="index"
|
wx:key="index"
|
||||||
title="{{ '标签' + item }}"
|
title="{{ '标签' + item }}"
|
||||||
>
|
>
|
||||||
<view class="content">
|
<view class="content" style="height: 400px;">
|
||||||
{{ '内容' + item }}
|
{{ '内容' + item }}
|
||||||
</view>
|
</view>
|
||||||
</van-tab>
|
</van-tab>
|
||||||
|
@ -67,6 +67,10 @@ VantComponent({
|
|||||||
offsetTop: {
|
offsetTop: {
|
||||||
type: Number,
|
type: Number,
|
||||||
value: 0
|
value: 0
|
||||||
|
},
|
||||||
|
scrollTop: {
|
||||||
|
type: Number,
|
||||||
|
value: 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -91,6 +95,7 @@ VantComponent({
|
|||||||
lineHeight: 'setLine',
|
lineHeight: 'setLine',
|
||||||
active: 'setActiveTab',
|
active: 'setActiveTab',
|
||||||
animated: 'setTrack',
|
animated: 'setTrack',
|
||||||
|
scrollTop: 'onScroll',
|
||||||
offsetTop: 'setWrapStyle'
|
offsetTop: 'setWrapStyle'
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -102,8 +107,6 @@ VantComponent({
|
|||||||
this.setLine();
|
this.setLine();
|
||||||
this.setTrack();
|
this.setTrack();
|
||||||
this.scrollIntoView();
|
this.scrollIntoView();
|
||||||
this.observerTabScroll();
|
|
||||||
this.observerContentScroll();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
destroyed() {
|
destroyed() {
|
||||||
@ -319,55 +322,39 @@ VantComponent({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
observerTabScroll() {
|
// adjust tab position
|
||||||
|
onScroll(scrollTop) {
|
||||||
if (!this.data.sticky) return;
|
if (!this.data.sticky) return;
|
||||||
|
|
||||||
const { offsetTop } = this.data;
|
const { offsetTop } = this.data;
|
||||||
wx.createIntersectionObserver(this, {
|
|
||||||
thresholds: [1]
|
|
||||||
}).relativeToViewport().observe('.van-tabs', result => {
|
|
||||||
const { top } = result.boundingClientRect;
|
|
||||||
let position = '';
|
|
||||||
|
|
||||||
if (offsetTop > top) {
|
this.getRect('.van-tabs').then(rect => {
|
||||||
position = 'top';
|
const { top, height } = rect;
|
||||||
}
|
|
||||||
|
|
||||||
this.$emit('scroll', {
|
this.getRect('.van-tabs__wrap').then(rect => {
|
||||||
scrollTop: top + offsetTop,
|
const { height: wrapHeight } = rect;
|
||||||
isFixed: position === 'top'
|
let position = '';
|
||||||
|
|
||||||
|
if (offsetTop > top + height - wrapHeight) {
|
||||||
|
position = 'bottom';
|
||||||
|
} else if (offsetTop > top) {
|
||||||
|
position = 'top';
|
||||||
|
}
|
||||||
|
|
||||||
|
this.$emit('scroll', {
|
||||||
|
scrollTop: scrollTop + offsetTop,
|
||||||
|
isFixed: position === 'top'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (position !== this.data.position) {
|
||||||
|
this.set({
|
||||||
|
position
|
||||||
|
}, () => {
|
||||||
|
this.setWrapStyle();
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.setPosition(position);
|
|
||||||
});
|
});
|
||||||
},
|
|
||||||
|
|
||||||
observerContentScroll() {
|
|
||||||
if (!this.data.sticky) return;
|
|
||||||
|
|
||||||
const { offsetTop } = this.data;
|
|
||||||
wx.createIntersectionObserver(this).relativeToViewport().observe('.van-tabs__content', result => {
|
|
||||||
const { top } = result.boundingClientRect;
|
|
||||||
let position = '';
|
|
||||||
|
|
||||||
if (result.intersectionRatio <= 0) {
|
|
||||||
position = 'bottom';
|
|
||||||
} else if (offsetTop > top) {
|
|
||||||
position = 'top';
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setPosition(position);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
setPosition(position) {
|
|
||||||
if (position !== this.data.position) {
|
|
||||||
this.set({
|
|
||||||
position
|
|
||||||
}, () => {
|
|
||||||
this.setWrapStyle();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user