如何解决Blazor Webassembly 中的 JavaScript 免费 Bootstrap Carousel
我是 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 文件。
解决方法
经过广泛的研究,现在我可以在不使用 JavaScript 的情况下在 blazor 中自动滚动轮播。我还让上一个和下一个按钮工作并添加了该功能:当用户单击上一个或下一个时,它进入手动模式并自动停止滚动。以下是我的解决方案:
@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;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。