vue3 页面滚动

2023-10-17  本文已影响0人  焚心123
const pageEle = ref(null);
const data = reactive({
    scrollTop: 0,
});
onMounted(() => {
            // 首页滚动到当前的某个位置,跳转页面在返回后还在当前的位置
            pageEle.value.addEventListener('scroll', (e) => {
                // console.log('滚动高度', e.target.scrollTop);
                data.scrollTop = e.target.scrollTop;
            });
            router.beforeEach((to, from, next) => {
                console.log('from', from);
                if (from.path) {
                    setTimeout(() => {
                        pageEle.value.scrollTop = data.scrollTop;
                    }, 0);
                }

                next();
            });
        });
        onUnmounted(() => {
            pageEle.value.removeEventListener('scroll', (e) => {
                data.scrollTop = e.target.scrollTop;
            });
        });
    const productEle = ref(null);
    const product3 = ref(null);            
        watch(
            () => route.query,
            (newV) => {
                console.log('new', newV);
                if (newV.hasOwnProperty('num')) {
                    data.num = newV.num;
                    console.log(data.num, 'num');
                } else if (newV.hasOwnProperty('type')) {
                    //首页点击后页面滚动到某一个地方
                    data.num = 0;
                    if (newV.type == 3) {
                        setTimeout(() => {
                            productEle.value.scrollTop = product3.value.offsetTop - 50;
                        }, 50);
                    }
                }
            },
            {
                deep: true,
                immediate: true,
            }
        );
上一篇下一篇

猜你喜欢

热点阅读