底部的Bootstrap 4轮播字幕

时间:2019-04-23 03:36:35

标签: css bootstrap-4

我正在尝试创建一个Bootstrap 4转盘,该转盘将在图像下方而不是顶部显示标题。我能够做到这一点,但是我遇到的问题是,居中时控件考虑了标题div的高度,因此垂直对齐处于关闭状态。

任何有关如何解决此问题的建议将不胜感激。

Codeply:http://codeply.com/go/lTDzvXp7IP

HTML

        <section class="media-gallery bg-light">            
        <div class="container">
            <div class="row d-flex justify-content-center">
                <div class="col-12 col-lg-11">
                    <div id="carouselExampleControls" class="carousel slide captioned" data-ride="carousel">
                        <div class="carousel-inner">
                            <div class="carousel-item active">
                                <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=16" alt="First slide">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image. I wonder what happens if this caption is very long? For example, what does it do to the credit?</p>
                                </div>
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=136" alt="First slide">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image.</p>
                                </div>
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=316" alt="First slide">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image. This caption is slightly different than the other, though idk why.</p>
                               </div>
                           </div>
                       </div>
                       <a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
                           <span class="sr-only">Next</span>
                       </a>
                   </div>
               </div>
           </div>
       </div>       
   </section>

CSS

.media-gallery {
    font-family: "Arial";
    font-size: 14px;
}

.carousel-caption {
    position: relative;
    border: 1px solid black;
    top: 0;
    left: 0;
    color: black;
    text-align: left;
    padding: 20px;
}

.carousel .carousel-control-prev, .carousel .carousel-control-next {
    font-size: 30px;
    background: none;
}

3 个答案:

答案 0 :(得分:1)

我已经更新了轮播控件的绝对位置,使其水平居中。现在,控件将不会在任何位置重叠字幕。

jQuery(function ($) {
    $('.carousel').carousel();
    var caption = $('div.carousel-item:nth-child(1) .media-content');
    $('.new-caption-area').html(caption.html());
    caption.css('display', 'none');

    $(".carousel").on('slide.bs.carousel', function (evt) {
        var caption = $('div.carousel-item:nth-child(' + ($(evt.relatedTarget).index() + 1) + ') .media-content');
        $('.new-caption-area').html(caption.html());
        caption.css('display', 'none');
    });
});
.media-gallery {
    font-family: "Arial";
    font-size: 14px;
}

.carousel-caption {
    position: relative;
    border: 1px solid black;
    top: 0;
    left: 0;
    color: black;
    text-align: left;
    padding: 20px;
}

.carousel .carousel-control-next,
.carousel .carousel-control-prev {
    z-index: 999;
    width: 45px;
    height: 45px;
    font-size: 30px;
    background: rgba(255, 255, 255, 0.25) none repeat scroll 0 0;
    border-radius: 100%;
    padding: 0 0 10px 0px;
    line-height: 20px;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}

.carousel .carousel-control-prev,
.carousel .carousel-control-next {
    top: 50%;
    bottom: 50%;
    width: 5%;
    opacity: 1;
    position: absolute;
}

.carousel .carousel-control-prev,
.carousel .carousel-control-next {
    font-size: 30px;
    background: none;
}

.carousel .carousel-control-next {
    right: 20px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<section class="media-gallery bg-light">
    <div class="container">
        <div class="row d-flex justify-content-center">
            <div class="col-12 col-lg-11">
                <div id="carouselExampleControls" class="carousel slide captioned" data-ride="carousel">
                    <div class="carousel-inner">
                        <div class="carousel-item active">
                            <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=16" alt="First slide">
                            <div class="media-content">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image. I wonder what happens if this caption is very long? For example, what does it do to the credit?</p>
                                </div>
                            </div>
                        </div>
                        <div class="carousel-item">
                            <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=136" alt="First slide">
                            <div class="media-content">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image.</p>
                                </div>
                            </div>
                        </div>
                        <div class="carousel-item">
                            <img class="d-block w-100" src="https://loremflickr.com/1000/667?r=316" alt="First slide">
                            <div class="media-content">
                                <p class="media-credit"><i class="fas fa-camera"></i> <span class="name">Person</span> / <span class="agency">Agency</span></p>
                                <div class="media-caption">
                                    <p>This is paragraph text that is the caption of the image. This caption is slightly different than the other, though idk why.</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                    </a>
                </div>
                <div class="new-caption-area"></div>
            </div>
        </div>
    </div>
</section>

答案 1 :(得分:0)

我使用的是 Bootstrap 4.5.2,这对我有用。在您的图片下方创建一个空段落块,它会在图片下方推送带有指示器的标题:

<!-- Wrapper for carousel items -->    
<div class="carousel-inner">
    <div class="carousel-item active">
        <img class="d-block w-100" src="my-image.png" alt="First slide">
        <p id="block-under-image"></p>
        <div class="carousel-caption">
            <p>My text goes here.</p>
        </div>
    </div>
</div>

CSS:

#block-under-image {
    background-color: black;
    height: 9em;
}

答案 2 :(得分:0)

你只需要使用适当的 z-index,默认值为 1

.carousel-control-next, .carousel-control-prev {
    z-index: 10000001;
}