如何在Blazor中使用Bootstrap Carousel [英] How to use Bootstrap Carousel in Blazor
问题描述
Blazor中的新手.需要尝试如何在Blazor中使用BS轮播.
newbie in Blazor. Need to try out how to use BS carousel in Blazor.
我在Default Blazor应用程序中使用了以下代码.但这是行不通的.我需要做什么?
I used below code in the Default Blazor app. But it does not work. What I need to do?
谢谢
-
我在Index.html
I added this line in the Index.html
< script src ="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
在计数器"页面中.我添加了以下内容:
In the Counter page. I added the following:
< div class ="container">
<div class="container">
<div class="carousel-inner">
<div class="item active">
<img src="la.jpg" alt="Los Angeles" style="width:100%;">
<div class="carousel-caption">
<h3>Los Angeles</h3>
<p>LA is always so much fun!</p>
</div>
</div>
<div class="item">
<img src="chicago.jpg" alt="Chicago" style="width:100%;">
<div class="carousel-caption">
<h3>Chicago</h3>
<p>Thank you, Chicago!</p>
</div>
</div>
<div class="item">
<img src="ny.jpg" alt="New York" style="width:100%;">
<div class="carousel-caption">
<h3>New York</h3>
<p>We love the Big Apple!</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
< /div >
-更新:Index.html
-- Update: Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>BlazorCarouselTest</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<script src="css/bootstrap/jquery-3.4.0.min.js" type="text/javascript"></script>
</head>
<body>
<app>Loading...</app>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html >
推荐答案
Carousel需要一些javascript代码,需要在页面加载时进行配置,但是在blazor页面中,没有人调用该组件的初始化.好消息是,您可以通过代码完成.
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.
三个简单步骤:
1.--创建并包含在blazor上使用javascript (您可以复制此代码并将其粘贴到index.html
页面的底部)
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.-稍微更改轮播的html
(从carousel-control-prev
div中删除href
.为上一个和下一个控件添加id
):
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.-首次渲染后调用上一个代码:
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;
}
}
仅此而已
让我们知道您的转盘是否正在移动!
Let us know if you your carousel is moving now!
已编辑
记住要包括在index.html上运行引导轮播所需的所有js:
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>
这篇关于如何在Blazor中使用Bootstrap Carousel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!