简体   繁体   English

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

[英]Show Carousel caption on image hover

I am using Bootstrap 4 carousel and I am trying to add an effect where carousel caption only displays on hover. 我正在使用Bootstrap 4轮播,并且尝试添加一种效果,其中轮播字幕仅在悬停时显示。 The problem is that when I hover over the image nothing is being displayed at all. 问题是,当我将鼠标悬停在图像上时,根本没有显示任何内容。

There are no errors in the console so I'm not sure what did I missed. 控制台中没有错误,所以我不确定错过了什么。 One thing that I thought that could be the issue was carousel sliding automatically but when I removed the feature I got the same results, nothing showing up on hover. 我认为可能是问题的一件事是轮播自动滑动,但是当我删除该功能时,却得到了相同的结果,悬停时没有任何显示。

The Carousel: 轮播:

<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: 使用Javascript:

<script type="text/javascript">

    $(document).ready(function () {

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

</script>

I'm not quite sure what did I missed in my java script. 我不太确定我在Java脚本中错过了什么。 Also if there is a better/cleaner way to do it purely in css any ideas would be highly accepted. 另外,如果有更好/更干净的方法来纯粹在CSS中进行操作,则任何想法都将被高度接受。

Thanks in advance. 提前致谢。

For a pure css alternative you can just have the caption fade in and out when you hover over the .carousel-inner like so: 对于纯CSS替代方案,当您将鼠标悬停在.carousel-inner上方时,只需使标题淡入和淡出即可,如下所示:

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

You can use mouseenter and mouseout to do the same. 您可以使用mouseenter和mouseout进行相同的操作。

$(document).ready(function () {

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

Possibly the selector used to access the image returns nothing as the selector syntax is incorrect. 由于选择器语法不正确,可能用于访问图像的选择器不返回任何内容。 See Attribute Selectors - jQuery 请参阅属性选择器-jQuery

You can find it out easily by printing it out in the console: 您可以通过在控制台中将其打印出来轻松找到它:

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

Try accessing the image in the following way: 尝试通过以下方式访问图像:

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

Please follow this. 请遵循此。 I hope you will get your solution. 希望您能找到解决方案。 And also follow Bootstrap 4 Carousel page. 并遵循Bootstrap 4 Carousel页面。 https://getbootstrap.com/docs/4.1/components/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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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