使用jquery在div中对齐图像

时间:2017-03-22 20:05:51

标签: javascript jquery

我有一个居中的div占据窗口宽度的80%,在div内部有一个图库.container img {min-width:200px;}使用jquery我想根据.container的宽度使图像合理化,对我有用的代码是:

$(window).on("resize", function(){

        // get the width of the container
        var albumContentWidth = $(".container").width();

        // as we have in our css the min-width of image is 200
        // see how many image can fit in each row
        var imagePerRow = parseInt(albumContentWidth / 200);

        // set the width of image using calc
        $(".container img").css("width", "calc(100% / #{imagePerRow})");

});

上面的代码工作正常,但出于某些原因..我不想使用calc()而是想用像素计算这个,所以我做了什么:

$(window).on("resize", function(){

        // get the width of the container
        var albumContentWidth = $(".container").width();

        // as we have in our css the min-width of image is 200
        // see how many image can fit in each row
        var imagePerRow = parseInt(albumContentWidth / 200);

        // instead of using calc() divide the width of container by the possible number of images per row
        var imageWidth = (albumContentWidth / imagePerRow).toFixed(2);

        // set the width of image using calc
        $(".container img").css("width", imageWidth);

});

然而,这不起作用,当我调整大小时,图像无法正确显示!

0 个答案:

没有答案