在图像悬停时显示轮播字幕

时间:2018-12-09 17:14:39

标签: javascript jquery html css bootstrap-4

我正在使用Bootstrap 4轮播,并且尝试添加一种效果,其中轮播字幕仅在悬停时显示。问题是,当我将鼠标悬停在图像上时,根本没有显示任何内容。

控制台中没有错误,所以我不确定我错过了什么。我认为可能是问题的一件事是轮播自动滑动,但是当我删除该功能时,却得到了相同的结果,悬停时没有任何显示。

轮播:

<div id="NewsCarousel" class="carousel slide default-div-top-padding" data-ride="carousel">

                    <!-- Indicators -->
                    <ul class="carousel-indicators">
                        <li data-target="#NewsCarousel" data-slide-to="0" class="active"></li>
                        <li data-target="#NewsCarousel" data-slide-to="1"></li>
                        <li data-target="#NewsCarousel" data-slide-to="2"></li>
                    </ul>

                    <!-- The slideshow -->
                    <div class="carousel-inner">

                        <div class="carousel-item active">
                            <img src="~/images/Resources/HomePage/knowledgeLatestNews.jpg" alt="imgAlt" style="width:650px;height:350px">
                            <div class="carousel-caption">
                                <a href="#"><h3>Los Angeles</h3></a>
                                <p>We had such a great time in LA!</p>
                            </div>
                        </div>

                        <div class="carousel-item">
                            <img src="~/images/Resources/HomePage/knowledgeLatestNews.jpg" alt="imgAlt" style="width:650px;height:350px">
                            <div class="carousel-caption">
                                <a href="#"><h3>Los rtr</h3></a>
                                <p>We had such a great time in LA!</p>
                            </div>
                        </div>

                        <div class="carousel-item">
                            <img src="~/images/Resources/HomePage/knowledgeLatestNews.jpg" alt="imgAlt" style="width:650px;height:350px">
                            <div class="carousel-caption">
                                <a href="#"><h3>Los Angeasdales</h3></a>
                                <p>We had such a great time in LA!</p>
                            </div>
                        </div>

                    </div>

                    <!-- Left and right controls -->
                    <a class="carousel-control-prev" href="#NewsCarousel" data-slide="prev">
                        <span class="carousel-control-prev-icon"></span>
                    </a>
                    <a class="carousel-control-next" href="#NewsCarousel" data-slide="next">
                        <span class="carousel-control-next-icon"></span>
                    </a>

 </div>

Javascript:

<script type="text/javascript">

    $(document).ready(function () {

        $('.carousel-caption').hide();
        $('img[alt = imgAlt').on('hover', function () {
            $('.carousel-caption').fadeIn();
        });
    });

</script>

我不太确定我在Java脚本中错过了什么。另外,如果有更好/更干净的方法来纯粹在CSS中进行操作,则任何想法都将被高度接受。

先谢谢了。

4 个答案:

答案 0 :(得分:3)

对于纯CSS替代项,您只需将鼠标悬停在.carousel-inner上就可以使标题淡入淡出,就像这样:

.carousel-caption{
  opacity:0;
  transition:500ms ease-in-out;
}
.carousel-item:hover .carousel-caption{
  opacity:1;
}

答案 1 :(得分:0)

您可以使用mouseenter和mouseout进行相同的操作。

$(document).ready(function () {

    $('.carousel-caption').hide();
    $('img[alt = imgAlt]').mouseenter(function () {
        $('.carousel-caption').show();
    }).mouseout(function(){
        $('.carousel-caption').hide();
    });
});

答案 2 :(得分:0)

由于选择器语法不正确,可能用于访问图像的选择器不返回任何内容。参见Attribute Selectors - jQuery

您可以通过在控制台中将其打印出来轻松找到它:

console.log($('img[alt = imgAlt'));

尝试通过以下方式访问图像:

$('img[alt="imgAlt"').on('hover', function () {
    $('.carousel-caption').fadeIn();
});

答案 3 :(得分:0)

请遵循此步骤。希望您能找到解决方案。并遵循Bootstrap 4 Carousel页面。 https://getbootstrap.com/docs/4.1/components/carousel/

.carousel-caption {
  opacity: 0;
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
  color: #fff;
}

.carousel-item:hover .carousel-caption {
  opacity: 1;
}
<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.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<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="https://via.placeholder.com/800x400" alt="First slide">
      <div class="carousel-caption d-none d-md-block">
        <h5>Slider Title 1</h5>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://via.placeholder.com/800x400" alt="Second slide">
      <div class="carousel-caption d-none d-md-block">
        <h5>Slider Title 1</h5>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://via.placeholder.com/800x400" alt="Third slide">
      <div class="carousel-caption d-none d-md-block">
        <h5>Slider Title 1</h5>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
      </div>
    </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>

相关问题