javascript - 在图像悬停时显示轮播标题

标签 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 中做到这一点,任何想法都会被高度接受。

提前致谢。

最佳答案

对于纯 css 替代方案,当您将鼠标悬停在 .carousel-inner 上时,您可以让标题淡入淡出,如下所示:

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

关于javascript - 在图像悬停时显示轮播标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694715/

相关文章:

javascript - 如何在 JavaScript 或 jQuery 中将 window.open 转换为 jQuery 加载(如灯箱)

asp.net-mvc - 使用 asp.net mvc/jquery 的全局 "ajax call"通知

javascript - 显示隐藏在 jquery 中基于复选框创建的 html

html - 无法使用动态绝对 DIV 向上滚动

javascript - 变换后的宽度/高度

javascript - 将变音符号传递给 jQuery UI 对话框

javascript - 类型错误 : Cannot read property 'nombre' of undefined

javascript - 获取 Jquery 模态框以显示在页面加载时

javascript - json 获取数据或调用函数

javascript - 运行 .each 循环以获取 div 内内容的值