简体   繁体   中英

Bootstrap carousel Slider active class and next/prev button not working

I'm using cshtml from Umbraco. I want to create slider on homepage. The image appear on page but "active" item not working. (THIS SOLVED)

Another problem is I want to add different caption for each slide. How to add that?

 @{ var images = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundSlider").ToList(); } <div id="myCarousel" class="carousel slide"> <div class="carousel-inner"> @for (var i = 0; i < images.Count; i++) { <div class="@(i < 1 ? " active":"") item" data-slide-number="@i"> <img src='@images[i].Url'> </div> } </div> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a> </div> 

The main problem seems to be your .carousel-item class, which was specified as .item instead. These have to be the same as they are specified in the documentation for everything to work as expected.

@{   
   var images = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundSlider").ToList();
}

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        @for (var i = 0; i < images.Count; i++)
        {
            <div class="@(i < 1 ? " active":"") carousel-item" data-slide-number="@i">
                <img src='@images[i].Url' class="d-block w-100">
            </div>  
        }
    </div>
    <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
    <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>

For captioning, this is a suitable Bootstrap docs example that would help you:

@{   
var images = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundSlider").ToList();
}

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        @for (var i = 0; i < images.Count; i++)
        {
            <div class="@(i < 1 ? " active":"") carousel-item" data-slide-number="@i">
                <img src='@images[i].Url' class="d-block w-100">
                <div class="carousel-caption d-none d-md-block">
                <h5>@images[i].Title</h5>
                <p>@images[i].Description</p>
                </div>
            </div>  
        }
    </div>
    <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
    <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>

In the example above I'm assuming your image title is in your Title property and caption is in Description . Feel free to amend these as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM