Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery添加图像标题_Jquery_Html_Css - Fatal编程技术网

使用JQuery添加图像标题

使用JQuery添加图像标题,jquery,html,css,Jquery,Html,Css,我正在尝试向div添加一个图像标题,下面的代码运行良好,并且符合我的要求,但是我想请您建议将代码缩短一点:)我相信有更好的方法可以做到这一点,但我找不到它 提前谢谢 HTML CSS 您可以清理javascript代码。例如: $('.block').hover(function() { $(this).find('.caption').animate({ "margin-left": "0px" }); }, function() { $(this).fin

我正在尝试向div添加一个图像标题,下面的代码运行良好,并且符合我的要求,但是我想请您建议将代码缩短一点:)我相信有更好的方法可以做到这一点,但我找不到它

提前谢谢

HTML

CSS


您可以清理javascript代码。例如:

$('.block').hover(function() {
  $(this).find('.caption').animate({
     "margin-left": "0px"       
  });
}, function() {
  $(this).find('.caption').animate({
     "margin-left": "-200px",
  });
});
希望能有帮助


关于。

无需使用单独的类。使用公共类,然后查找标题

$(".block").hover(
    function () {
        $(this).find(".caption").animate({
            "margin-left": "0px"
        });
    },
    function () {
        $(this).find(".caption").animate({
            "margin-left": "-200px"
        });
    }
);

有了cssThanks很多:)这段工作代码看起来好多了!也感谢您的帮助,代码运行良好:)
.caption{
height: 150px;
width: 200px;
text-align: center;
margin-left: -200px;
margin-top: 350px;
background: black;
color: white;
}
$('.block').hover(function() {
  $(this).find('.caption').animate({
     "margin-left": "0px"       
  });
}, function() {
  $(this).find('.caption').animate({
     "margin-left": "-200px",
  });
});
$(".block").hover(
    function () {
        $(this).find(".caption").animate({
            "margin-left": "0px"
        });
    },
    function () {
        $(this).find(".caption").animate({
            "margin-left": "-200px"
        });
    }
);