Javascript 使用jquery对div内的图像进行对齐
我有一个居中的div,占据了窗口宽度的80%,在该div内有一个gallery imagesJavascript 使用jquery对div内的图像进行对齐,javascript,jquery,Javascript,Jquery,我有一个居中的div,占据了窗口宽度的80%,在该div内有一个gallery images.container img{min width:200px;}使用jquery我想根据.container的宽度调整图像,对我有效的代码如下: $(window).on("resize", function(){ // get the width of the container var albumContentWidth = $(".container").width
.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);
});
然而,这是不工作,图像没有得到正确的正当性,当我调整 问题是,如果可以用简单的CSS来实现这一点,为什么要使用jquery呢flexbox@ArtemSkyflex的缺点是,如果行中只有一个元素,它将使行居中,我已经用
验证内容:间距尝试过flex,但正如我所说的,当每行只有一到两个元素时,它确实会使元素居中…@DaniP我想我的问题还不够清楚,我写它的时候头疼,哈哈,我会用jsfiddle再次重写它,所以你可以看到:)我不想使用css“flex”也不是媒体的质疑,因为两者都不能实现我想要做的