简体   繁体   English

Javascript / jQuery在加载时随机调整图片大小?

[英]Javascript/jQuery to resize randomly the pictures on load?

Instead of creating thumbnails for my images, I would like to show the original images in my page. 我想在我的页面中显示原始图像,而不是为我的图像创建缩略图。 For viewing purposes, I still need to resize these images but instead of adding a width:200px on them, I want to display each image in a div container but with size randomly chosen from three choices. 出于查看目的,我仍然需要调整这些图像的大小,但不是在它们上添加width:200px ,我想在div容器中显示每个图像,但是从三个选项中随机选择大小。

These 3 types of div containers (and therefore the images appearance) will have 3 different sizes, all with the same width. 这3种类型的div容器(因此图像外观)将具有3种不同的尺寸,所有尺寸都相同。 300x100, 300x200, 300x400. 300x100,300x200,300x400。

Of course, upon refresh, the images may have a different size now. 当然,在刷新时,图像现在可以具有不同的大小。

How can I do this ? 我怎样才能做到这一点 ?

Set each height this way: 以这种方式设置每个高度:

var heights = [100, 200, 400];
var height = heights[Math.ceil(Math.random()*3)-1];

I guess you would want to iterate through the containers... 我猜你想要遍历容器......

var heights = ["100px", "200px", "400px"];
$(".container").each(function(i,e) {
    var height = heights[Math.ceil(Math.random()*3)-1];
    $(e).css('height', height);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM