Carousel needs a bit of javascript code, it needs to be configured on page load, but, into a blazor page, nobody calls the initialization for the component. The good news is that you can do it by code.
Three easy steps:
1.- Create and include javascript on blazor (you can copy paste this code at bottom of your index.html
page )
<script>
window.initializeCarousel = () =>
{
$('#carouselExampleIndicators').carousel({interval: 2000});
//see step 2 to understand these news id's:
$('#carouselExampleIndicators-prev').click (
() => $('#carouselExampleIndicators').carousel('prev') );
$('#carouselExampleIndicators-next').click (
() => $('#carouselExampleIndicators').carousel('next') );
}
</script>
2.- Change a bit the carousel's html
( remove href
from carousel-control-prev
divs. Add an id
for prev and next controls ):
<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>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-50" src="..." alt="Pepa Pig">
</div>
<div class="carousel-item">
<img class="d-block w-50" src="..." alt="Sponge Bob">
</div>
</div>
<a id="carouselExampleIndicators-prev"
class="carousel-control-prev" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a id="carouselExampleIndicators-next"
class="carousel-control-next" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
3.- Call the previous code after first render:
@page "/counter"
@inject IJSRuntime JsRuntime;
...
@functions {
int currentCount = 0;
bool firstRender = true;
...
protected async override Task OnAfterRenderAsync()
{
if (firstRender)
{
await JsRuntime.InvokeAsync<object>("initializeCarousel");
firstRender=false;
}
}
That's all:
Let us know if you your carousel is moving now!
Edited
Remember to include all js needed to run bootstrap carousel on index.html:
<body>
<app>Loading...</app>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="_framework/blazor.webassembly.js"></script>
<script>
window.initializeCarousel = () =>
{
$('#carouselExampleIndicators').carousel({interval: 2000})
$('#carouselExampleIndicators-prev').click (
() => $('#carouselExampleIndicators').carousel('prev') );
$('#carouselExampleIndicators-next').click (
() => $('#carouselExampleIndicators').carousel('next') );
}
</script>
</body>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…