最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

in blazor app using javascript setScrollPos and getScrollPos to maintain scroll on list page - Stack Overflow

programmeradmin0浏览0评论

In my Blazor app, I'm using the following JavaScript to maintain scrolling on my list page after returning from details page. The user experience is not the greatest, it starts at top of the page then jumps to stored scroll position.

Is there a way to make that a better user experience? For example don't show the screen until it's back at the scroll position.

<script>
    window.resetScroll = () => {
        window.scrollTo(0, 0);
    };

    window.storeScrollPosition = () => {
        sessionStorage.setItem('scrollPosition', window.scrollY);
    };

    window.restoreScrollPosition = () => {
        const scrollPosition = sessionStorage.getItem('scrollPosition');
        if (scrollPosition) {
            window.scrollTo(0, parseInt(scrollPosition));
            sessionStorage.removeItem('scrollPosition');
        }
    };
</script>

In my Blazor app, I'm using the following JavaScript to maintain scrolling on my list page after returning from details page. The user experience is not the greatest, it starts at top of the page then jumps to stored scroll position.

Is there a way to make that a better user experience? For example don't show the screen until it's back at the scroll position.

<script>
    window.resetScroll = () => {
        window.scrollTo(0, 0);
    };

    window.storeScrollPosition = () => {
        sessionStorage.setItem('scrollPosition', window.scrollY);
    };

    window.restoreScrollPosition = () => {
        const scrollPosition = sessionStorage.getItem('scrollPosition');
        if (scrollPosition) {
            window.scrollTo(0, parseInt(scrollPosition));
            sessionStorage.removeItem('scrollPosition');
        }
    };
</script>
Share Improve this question asked Mar 13 at 6:05 RodRod 15.5k35 gold badges134 silver badges264 bronze badges 1
  • I guess you can show some kind of loader when there is scrollPosition set in the sessionStorage and remove it after the window scrolls there. – CodeThing Commented Mar 13 at 6:27
Add a comment  | 

1 Answer 1

Reset to default 0

Based on your post

You may try :

Blazor.addEventListener('enhancedload', () =>
{
   const scrollPosition = sessionStorage.getItem('scrollPosition');
   const path=window.location.pathname;

   if (scrollPosition&path=={targetpath}) {
    window.scrollTo(0, parseInt(scrollPosition));
    sessionStorage.removeItem('scrollPosition');
}

});
发布评论

评论列表(0)

  1. 暂无评论