【问题标题】:Blazor programmatically scrolling inside div (or component)Blazor programmatically scrolling inside div (or component)
【发布时间】:2022-12-26 21:53:44
【问题描述】:

I want to scroll inside a list in a div (or component) by c#/js -call. But what happens is that the hole page is scrolling to the target element.I have thisgui:

When youclick on the button, the content of the list-div (blue)shouldscroll to the entry 110 like this:

Butwhat's actually happensis that the hole page is scrolling like that:

In other words: The header and the rest of the page should keep the position. Only inside the list-div should be scrolled.

How can I programmatically scroll inside a div (or component)?

index.html:

<head>
<script type="text/javascript">
    ScrollElementIntoView = element => element.scrollIntoView();
</script> ...

index.razor:

@page "/"
@inject IJSRuntime JsRuntime

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<div>
    <h3>My List</h3>
    <button @onclick="GoToNextDate">Go to 110</button>
</div>
<br />

<div style="height: 500px; width: 200px; background-color: powderblue; overflow:auto;">
    <ul>
        @foreach (var x in Enumerable.Range(0, 300))
        {
            if (x == 110)
            {
                <span @ref="NextDate"></span>
            }
            <li>@x</li>
        }
    </ul>>
</div>

@code {
    ElementReference NextDate { get; set; }
    List<Func<Task>> AfterRenderAsyncJobs = new();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        while (AfterRenderAsyncJobs.Any())
        {
            var job = AfterRenderAsyncJobs.First();
            AfterRenderAsyncJobs.Remove(job);
            await job.Invoke();
        }
    }

    private void GoToNextDate()
    {
        AfterRenderAsyncJobs.Add(ScrollToNextDate);
        StateHasChanged();
    }

    private async Task ScrollToNextDate()
    {
        await JsRuntime.InvokeVoidAsync("ScrollElementIntoView", NextDate);
    }
}

MainLayout.razor:

@inherits LayoutComponentBase

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

【问题讨论】:

标签: blazor blazor-webassembly


【解决方案1】:

I tested with the different options of scrollIntoView, but nothing was giving satisfactory results for what you want.

This is how I changed your code to work:

    <script type="text/javascript">
        ScrollElementIntoView = element => {
            var target = document.getElementById(element.id);
            target.parentNode.parentNode.scrollTop = target.offsetTop - target.parentNode.parentNode.offsetTop;
        }
    </script>
<div style="height: 500px; width: 200px; background-color: powderblue; overflow:auto;">
    <ul>
        @foreach (var x in Enumerable.Range(0, 300))
        {
            if (x == 110)
            {
                <span @ref="NextDate" id="element-110"></span>
            }
            <li>@x</li>
        }
    </ul>
</div>

Try it and see if it works for you as well.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2022-12-01
    • 2022-11-29
    • 2022-12-22
    • 2011-04-19
    • 1970-01-01
    • 2022-11-09
    相关资源
    最近更新 更多