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

标签 javascript jquery

我不想为图像创建缩略图,而是想在页面中显示原始图像。出于查看目的,我仍然需要调整这些图像的大小,但我不想在它们上添加 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/

相关文章:

javascript - 登录后在 chrome 扩展中弹出窗口

jquery - 对象不支持此属性或方法: JavaScript

javascript - 在 x 次页面浏览后运行 javascript

javascript - 定位变量然后使用 CSS 选择器

javascript - 网络错误: XMLHttpRequest Exception 101 in Chrome

javascript - 滚动到 DataTables jQuery 插件中选定的行

javascript - 使用 apply 调用 console.log

javascript - respond.js 设置教程

javascript - 剥离 Jquery Cookie 脚本

jquery - CSS hover 轻拂下图