我不想为图像创建缩略图,而是想在页面中显示原始图像。出于查看目的,我仍然需要调整这些图像的大小,但我不想在它们上添加 width:200px
,而是想在 div 容器中显示每个图像,但大小从三个选项中随机选择。
这 3 种类型的 div 容器(以及图像外观)将具有 3 种不同的尺寸,且宽度均相同。 300x100、300x200、300x400。
当然,刷新后,图像现在可能具有不同的大小。
我该怎么做?
最佳答案
这样设置每个高度:
var heights = [100, 200, 400];
var height = heights[Math.ceil(Math.random()*3)-1];
我猜你会想要迭代容器......
var heights = ["100px", "200px", "400px"];
$(".container").each(function(i,e) {
var height = heights[Math.ceil(Math.random()*3)-1];
$(e).css('height', height);
});
关于Javascript/jQuery 随机调整加载图片的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11304298/