JavaScript 免费 Bootstrap Blazor Webassembly 中的旋转木马

JavaScript free Bootstrap Carousel in Blazor Webassembly

我是 Blazor 新手。

我在 .NET 5 的 Blazor Webassembly 项目中创建了一个旋转木马。它不会自动滑动,并且它的上一个和下一个按钮不起作用。

没有javascript,请指教如何解决。

下面是我的代码。

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block w-100" src="/images/slide/slide-1.jpg" alt="First slide">
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="/images/slide/slide-2.jpg" alt="Second slide">
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="/images/slide/slide-3.jpg" alt="Third slide">
        </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

PS:我正在使用 VS 2019 和 .NET 5 生成的默认项目模板,没有包含任何额外的 javascript 文件。

经过大量研究,现在我可以在 blazor 中自动滚动轮播而无需 javascript。我还让 prev 和 next 按钮工作并添加了功能:当用户单击 prev 或 next 时,它会进入手动模式并自动停止滚动。以下是我的解决方案:

@page "/"
@using System.Threading;

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="@firstSlideCss"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1" class="@secondSlideCss"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2" class="@thirdSlideCss"></li>
    </ol>
    <div class="carousel-inner">
        <div class="carousel-item @firstSlideCss">
            <img class="d-block w-100" src="/images/slide/slide-1.jpg" alt="First slide">
        </div>
        <div class="carousel-item @secondSlideCss">
            <img class="d-block w-100" src="/images/slide/slide-2.jpg" alt="Second slide">
        </div>
        <div class="carousel-item @thirdSlideCss">
            <img class="d-block w-100" src="/images/slide/slide-3.jpg" alt="Third slide">
        </div>
    </div>
    <a class="carousel-control-prev" href="" role="button" data-slide="prev" @onclick="()=>Manually(true)">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="" role="button" data-slide="next" @onclick="()=>Manually(false)">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

@code{
    string firstSlideCss = "active";
    string secondSlideCss = "";
    string thirdSlideCss = "";

    int currentPosition = 0;
    int currentSlide = 0;

    CancellationTokenSource cts;
    CancellationToken ct;

    protected override async Task OnInitializedAsync()
    {
        cts = new CancellationTokenSource();
        ct = cts.Token;
        await Walkthrough(ct);
    }

    public async Task Walkthrough(CancellationToken ct)
    {
        while (!ct.IsCancellationRequested)
        {
            await Task.Delay(2500, ct);
            currentPosition++;
            ChangeSlide();
            await InvokeAsync(() => this.StateHasChanged());
        }
    }

    public void Manually(bool backwards)
    {
        cts.Cancel();

        if (backwards)
            currentPosition--;
        else
            currentPosition++;

        ChangeSlide();
    }

    private void ChangeSlide()
    {
        currentSlide = Math.Abs(currentPosition % 3);

        switch (currentSlide)
        {
            case 0:
                firstSlideCss = "active";
                secondSlideCss = "";
                thirdSlideCss = "";
                break;
            case 1:
                firstSlideCss = "";
                secondSlideCss = "active";
                thirdSlideCss = "";
                break;
            case 2:
                firstSlideCss = "";
                secondSlideCss = "";
                thirdSlideCss = "active";
                break;
            default:
                break;
        }
    }
}