我有这段代码,用于在一堆div中垂直居中显示图像。
function centerImages(parent, image) {
var parent_height = $(image).parent().height();
var image_height = $(image).height();
var top_margin = (parent_height - image_height)/2;
$(image).css( 'margin-top' , top_margin);
}
centerImages(".clients li", ".clients li img");。。但它似乎不起作用。
发布于 2012-08-01 18:26:40
试试这个吧。
function centerImages(image) {
var parent_height = $(image).parent().height();
var image_height = $(image).height();
var top_margin = (parent_height - image_height) / 2;
$(image).css( 'margin-top' , top_margin);
}
$(".clients li img").each(function() {
centerImages(this);
});您实际上并没有传入图像,只是传入了类选择器。上面的代码选择了所有相关的图像,并将它们传入--不需要使用parent参数。
发布于 2012-08-01 18:32:56
试试这个,
div.clients li img { vertical-align:middle; }发布于 2012-08-01 18:29:11
如果您的div具有相同的高度并且只包含图像,则这是一个纯粹的CSS解决方案:
http://jsfiddle.net/Tpy2w/
相关CSS
div {
width: 300px;
height : 300px;
line-height: 300px;
text-align: center;
}
div img {
vertical-align: middle;
}只需为该div设置一个height和相同的line-height。然后在图像上应用vertical-align: middle
https://stackoverflow.com/questions/11757229
复制相似问题