javascript - 如何在图像上滑动切换文本?

标签 javascript jquery html css

我编写了这段有效的代码。基本上,当用户将鼠标悬停在图像上时,文本会出现在图像上。

这是一个画廊,所以我需要使用 $(this).children 来显示正确的元素。

我不明白的是,我无法制作 h2 .nom_realisation 幻灯片工具。我尝试了几件事但没有成功。 我相信这很简单。如果有人能指出我正确的方向?

演示:http://jsfiddle.net/Vinyl/725hcc62/1/

代码:

CSS

.hide {
    display : none;
}
.text {
    z-index:100;
    position:absolute;
    top:0;
    left:0;
    height:100%;
    width: 100%;
    text-align: center;
    display: table;
    background: rgb(134, 0, 0);
    /* Fall-back for browsers that don't support rgba */
    background: rgba(134, 0, 0, .7);
}
h2.nom_realisation {
    color: #ffffff !important;
    font-family:'Oswald', sans-serif;
    font-size: 30px !important;
    font-weight: 300 !important;
    text-transform: uppercase;
    text-decoration: none !important;
    margin: 0;
    padding: 0;
    vertical-align: middle;
    display: table-cell;
    width: 100%;
}
h2.nom_realisation a, h2.nom_realisation a:hover {
    color: #ffffff !important;
    text-decoration: none;
}
.container_img img {
    position:absolute;
    left:0;
    top:0;
}
.container_img {
    height:232px;
    width:232px;
    position:relative;
}
.image_test {
    width:232px;
    height: auto;
}

HTML

<div class="container_img">
    <div class="container_nom_realisation hide">
        <div class="text">
             <h2 class="nom_realisation">Lorem ipsum</h2>

        </div>
    </div>
    <img class="image_test" src="https://www.google.fr/images/srpr/logo11w.png" />
</div>

JavaScript/jQuery

(function ($) {

    $(document).ready(function () {


        $(".container_img").hover(function () {

            $(this).children('.container_nom_realisation').show('slow');
            $(this).children('.text').slideToggle('slow');

        }, function () {
            $(this).children("img").fadeTo(200, 1)
                .end().children(".text").hide();
            $(this).children('.container_nom_realisation').hide('slow');
            //.end().children(".hover").slideToggle("slow");

        });
    });

})(jQuery);

最佳答案

  • .nom_realisation不是 .container_img 的 child , 所以你需要 .find()而不是 child 。
  • 您将无法为 table-cell 设置幻灯片动画元素。要么不要以这种方式显示它,要么(因为我假设你使用 table-cell 作为垂直中心,让另一个元素充当表格单元格包装你的 <h2> :

HTML

<div class="container_img">
    <div class="container_nom_realisation hide">
        <div class="text">
            <div class='table-cell'>
                 <h2 class="nom_realisation">Lorem ipsum</h2>

            </div>
        </div>
    </div>
    <img class="image_test" src="https://www.google.fr/images/srpr/logo11w.png" />
</div>

CSS

.hide {
    display : none;
}
.text {
    z-index:100;
    position:absolute;
    top:0;
    left:0;
    height:100%;
    width: 100%;
    text-align: center;
    display: table;
    background: rgb(134, 0, 0);
    /* Fall-back for browsers that don't support rgba */
    background: rgba(134, 0, 0, .7);
}
.table-cell {
    vertical-align: middle;
    display: table-cell;
}
h2.nom_realisation {
    color: #ffffff !important;
    font-family:'Oswald', sans-serif;
    font-size: 30px !important;
    font-weight: 300 !important;
    text-transform: uppercase;
    text-decoration: none !important;
    margin: 0;
    padding: 0;
    width: 100%;
}
h2.nom_realisation a, h2.nom_realisation a:hover {
    color: #ffffff !important;
    text-decoration: none;
}
.container_img img {
    position:absolute;
    left:0;
    top:0;
}
.container_img {
    height:232px;
    width:232px;
    position:relative;
}
.image_test {
    width:232px;
    height: auto;
}

JavaScript

(function ($) {
    $(document).ready(function () {
        $(".container_img").hover(function () {
            $(this).children('.container_nom_realisation').show('slow');
            $(this).find('.nom_realisation').slideToggle('slow');
        }, function () {
            $(this).children("img").fadeTo(200, 1)
                .end().children(".text").hide();
            $(this).children('.container_nom_realisation').hide('slow');
            //.end().children(".hover").slideToggle("slow");
        });
    });
})(jQuery);

JSFiddle

关于javascript - 如何在图像上滑动切换文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992343/

相关文章:

javascript - 如何使用 JQUERY 下载 img?

javascript - Bootstrap.js 选项卡不起作用

html - 滚动问题滚动不起作用

Jquery DOB 输入要验证的字段

javascript - React——点击按钮定时器开始计时

javascript - 将数学对象转换为实际对象

javascript - getBoundingClientRect() 不一致

javascript - @Html.Raw 生成的 javascript 无法正常工作

jquery - 如何将我的幻灯片放在 html 文档的中心?

javascript - 如何将 Javascript 字典传递给 Controller ​​,其中 C# 字典值是一个对象