[英]Vertically centering element (jQuery)
HTML: HTML:
<ul>
<li><a href="#"><img src="image1.jpg" /></a></li>
<li><a href="#"><img src="image2.jpg" /></a></li>
</ul>
LI is (60px x 60px) but the images inside have different heights. LI为(60px x 60px),但内部图像具有不同的高度。 I think they can be centered if the margin is set dynamically:
我认为,如果可以动态设置边距,则可以将它们居中:
margin-top = height-of-li - height-of-img / 2
How do I do that with jQuery. 我该如何使用jQuery。
Many thanks! 非常感谢!
var image_height = $('li').find('img').height();
var padding = (60 - image_height) / 2;
$('li').find('img').css('margin-top', padding+'px');
Could this help? 能帮上忙吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.