c# - 使用 Blazor 中的按钮向左/向右滚动

标签 c# razor blazor

我有几张图片排成一行。并非所有图像都可见。通过向右滚动,可以看到其他图像。我想通过单击按钮向右滚动。如何编写我的方法 ScrollRight 来实现我的目标?

<div style="display:flex;">
    <button @onclick="ScrollLeft">A</button>
    <div style="overflow:hidden;">
        <div style="display:flex;position:relative;bottom:-30px;overflow-y:auto;">
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <img width="300" height="300" src="https://www.dogstrust.org.uk/help-advice/_images/164742v800_puppy-1.jpg" />
            <!-->and so on/!-->
        </div>
    </div>
    <button @onclick="ScrollRight">B</button>
</div>


@code {

    public void ScrollLeft()
    {

    }

    public void ScrollRight()
    {

    }

}

最佳答案

我创建了一个 .js 文件:

function ScrollRight(Element, Width) {
    document.getElementById(Element).scrollBy({
        top: 0, left: Width * Math.floor((document.getElementById(Element).offsetWidth / Width)), behavior: "smooth"
    })
}
function ScrollLeft(Element, Width) {
    document.getElementById(Element).scrollBy({
        top: 0, left: -Width * Math.floor((document.getElementById(Element).offsetWidth / Width)), behavior: "smooth"
    })
}

添加了@inject IJSRuntime JsRuntime

@code {


    async Task ScrollRight()
    {
        await JsRuntime.InvokeVoidAsync(identifier: "ScrollRight", "scrollable-events", 255);
    }

    async Task ScrollLeft()
    {
        await JsRuntime.InvokeVoidAsync(identifier: "ScrollLeft", "scrollable-events", 255);
    }

}

在我的 .razor 文件中。

关于c# - 使用 Blazor 中的按钮向左/向右滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62821867/

相关文章:

c# - 为什么我不能将 List<T> 传递给通用构造函数参数,尽管所有约束都已满足?

c# - 使用多维字符串数组写入文件

c# - .Net Core API 无法使用 MSI 从 Azure CLI 获取 token

blazor - 在 Blazor JSInterop 中保留套管?

c# - Blazor 将输入值绑定(bind)到 oninput 不适用于 onkeypress

c# - 写一个大数据表到mysql数据库c#

asp.net-mvc-3 - 动态加载部分 View

asp.net-mvc-3 - 嵌入 MVC3 Razor View 中的脚本标签

asp.net-mvc - MVC3 一个 View 中的两个局部 View

async-await - Blazor 客户端刷新组件