我有这段代码,用于在一堆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参数。
https://stackoverflow.com/questions/11757229
复制相似问题