我有X个div,都设置为特定的高度和宽度.其中有一张图片,大小各不相同.我想找到图像的高度,将其除以2并将其设置为边距最高值,以便所有图像都在中心,如果这有意义的话?我试过制作一个小提琴只是我不确定如何去做...
你可以这样做
$(document).ready(function() {
$('.box img').each(function() {
var $this = $(this);
var parentHeight = $this.parent().height();
var thisHeight = $this.height();
var topmargin = (parentHeight - thisHeight) / 2;
$this.css("margin-top", topmargin);
});
})
Run Code Online (Sandbox Code Playgroud)
小提琴这里http://jsfiddle.net/R8QUL/6/