mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-05 19:41:45 +08:00
78 lines
1.6 KiB
TypeScript
78 lines
1.6 KiB
TypeScript
import { VantComponent } from '../common/component';
|
|
|
|
VantComponent({
|
|
props: {
|
|
zIndex: {
|
|
type: Number,
|
|
value: 99
|
|
},
|
|
offsetTop: {
|
|
type: Number,
|
|
value: 0
|
|
},
|
|
disabled: Boolean
|
|
},
|
|
|
|
data: {
|
|
wrapStyle: '',
|
|
containerStyle: ''
|
|
},
|
|
|
|
methods: {
|
|
setStyle() {
|
|
const { offsetTop, height, fixed, zIndex } = this.data;
|
|
|
|
if (fixed) {
|
|
this.setData({
|
|
wrapStyle: `top: ${offsetTop}px;`,
|
|
containerStyle: `height: ${height}px; z-index: ${zIndex};`
|
|
});
|
|
} else {
|
|
this.setData({
|
|
wrapStyle: '',
|
|
containerStyle: ''
|
|
});
|
|
}
|
|
},
|
|
|
|
observerContentScroll() {
|
|
const { offsetTop } = this.data;
|
|
const intersectionObserver = this.createIntersectionObserver({
|
|
thresholds: [0, 1]
|
|
});
|
|
this.intersectionObserver = intersectionObserver;
|
|
intersectionObserver.relativeToViewport({ top: -offsetTop });
|
|
intersectionObserver.observe(
|
|
'.van-sticky',
|
|
(res) => {
|
|
if (this.data.disabled) {
|
|
return;
|
|
}
|
|
// @ts-ignore
|
|
const { top, height } = res.boundingClientRect;
|
|
const fixed = top <= offsetTop;
|
|
|
|
this.$emit('scroll', {
|
|
scrollTop: top,
|
|
isFixed: fixed
|
|
});
|
|
|
|
this.setData({ fixed, height });
|
|
|
|
wx.nextTick(() => {
|
|
this.setStyle();
|
|
});
|
|
}
|
|
);
|
|
}
|
|
},
|
|
|
|
mounted() {
|
|
this.observerContentScroll();
|
|
},
|
|
|
|
destroyed() {
|
|
this.intersectionObserver.disconnect();
|
|
}
|
|
});
|