c# - Blazor JS Interopjavascript window events and functions not working - Stack Overflow

In a Blazor MAUI Hybrid app, I am trying to save the scroll position per page so that if returning to t

In a Blazor MAUI Hybrid app, I am trying to save the scroll position per page so that if returning to the page the saved scroll position is then reapplied.

I know that I should be saving it in session storage and per page, but I'm struggling to get the basic functionality to work.

One thing I tried throws the following exception:

Microsoft.JSInterop.JSException: window.scrollTop is not a function
TypeError: window.scrollTop is not a function
@inject NavigationManager NavManager
...
<script>
    function saveScrollPosition() {
        var scrollPosition = window.scrollTop();
        localStorage.setItem("scrollPosition", scrollPosition);
    }
    function restoreScrollPosition() {
        if (localStorage.scrollPosition) {
            var scrollPosition = localStorage.getItem("scrollPosition");
            window.scrollTop(scrollPosition);
        }
    }
</script>
...
@code{
    protected override async Task OnInitializedAsync()
    {
        NavManager.LocationChanged += OnLocationChanged;
    }

    private async void OnLocationChanged(object sender, LocationChangedEventArgs e)
    {
        await JsRuntime.InvokeVoidAsync("saveScrollPosition");
    }
}

And my previous attempt to register it at onbeforeunload and onload never fires.

<script>
    window.onbeforeunload = function() {
        var scrollPosition = window.scrollTop();
        localStorage.setItem("scrollPosition", scrollPosition);
    }
</script>

Does anyone have a pointer to what I'm missing? Basic window functions and events appear not to be working at all.

In a Blazor MAUI Hybrid app, I am trying to save the scroll position per page so that if returning to the page the saved scroll position is then reapplied.

I know that I should be saving it in session storage and per page, but I'm struggling to get the basic functionality to work.

One thing I tried throws the following exception:

Microsoft.JSInterop.JSException: window.scrollTop is not a function
TypeError: window.scrollTop is not a function
@inject NavigationManager NavManager
...
<script>
    function saveScrollPosition() {
        var scrollPosition = window.scrollTop();
        localStorage.setItem("scrollPosition", scrollPosition);
    }
    function restoreScrollPosition() {
        if (localStorage.scrollPosition) {
            var scrollPosition = localStorage.getItem("scrollPosition");
            window.scrollTop(scrollPosition);
        }
    }
</script>
...
@code{
    protected override async Task OnInitializedAsync()
    {
        NavManager.LocationChanged += OnLocationChanged;
    }

    private async void OnLocationChanged(object sender, LocationChangedEventArgs e)
    {
        await JsRuntime.InvokeVoidAsync("saveScrollPosition");
    }
}

And my previous attempt to register it at onbeforeunload and onload never fires.

<script>
    window.onbeforeunload = function() {
        var scrollPosition = window.scrollTop();
        localStorage.setItem("scrollPosition", scrollPosition);
    }
</script>

Does anyone have a pointer to what I'm missing? Basic window functions and events appear not to be working at all.

Share Improve this question asked Mar 26 at 19:14 ritocesuraritocesura 551 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This is an expected behavior. window.scrollTop() is a method in Jquery. If you want to use scrollTop, you must reference Jquery. For recording the scroll position and restoring the scroll position, you can use the ScrollTo method to achieve it.

Here is an example of recording the top position through two buttons in Maui Balzor and restoring it through the second button. You can refer to this code snippet.

@inject IJSRuntime JsRuntime
<button class="btn btn-primary" @onclick="SaveScrollPosition">Click me</button>
//Long Content Here
 
<button class="btn btn-primary" @onclick="ScrollTo">Click me</button>
<script>
    function saveScrollPosition() {
 
    localStorage.setItem("scrollPosition", window.scrollY);
    }
    function restoreScrollPosition() {
        if (localStorage.scrollPosition) {
            var scrollPosition = localStorage.getItem("scrollPosition");
            window.scrollTo(0,scrollPosition);
        }
    }
</script>
@code {
 
    private async void SaveScrollPosition()
    {
        await JsRuntime.InvokeVoidAsync("saveScrollPosition");
    }
    private async void ScrollTo()
    {
        await JsRuntime.InvokeVoidAsync("restoreScrollPosition");
    }
 
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744130391a4559813.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信