I have created a code to add image captions to different div blocks, and it works as intended. However, I am looking for ways to optimize the code and make it shorter. If you have any advice on how to improve it, please let me know!
Thank you in advance.
HTML
<div id="firstBlock" class="block"><p class="caption c1">Description</p></div>
<div id="secondBlock" class="block"><p class="caption c2">Description</p></div>
<div id="thirdBlock" class="block"><p class="caption c3">Description</p></div>
<div id="fourthBlock" class="block"><p class="caption c4">Description</p></div>
JQuery
function animateCaption(captionClass){
$(captionClass).hover(
function(){
$(this).animate({"margin-left": "0px"});
},
function(){
$(this).animate({"margin-left": "-200px"});
}
);
}
animateCaption(".caption.c1");
animateCaption(".caption.c2");
animateCaption(".caption.c3");
animateCaption(".caption.c4");
CSS
.caption{
height: 150px;
width: 200px;
text-align: center;
margin-left: -200px;
margin-top: 350px;
background: black;
color: white;
}