Centering images in a div vertically

后端 未结 3 467
太阳男子
太阳男子 2021-01-28 12:32

I have this code for centering vertically the images in a bunch of divs.

function centerImages(parent, image) {
    var parent_height = $(image).parent().height(         


        
3条回答
  •  天涯浪人
    2021-01-28 13:02

    Try this instead...

    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);
    });
    

    You weren't actually passing in images, just the class selector. The above selects all relevant images and passes them in - there's no need for the parent parameter.

提交回复
热议问题