jquery - 使用 Jquery 随机设置图像的宽度

标签 jquery css random width

我希望将图像的宽度设置为 25% 到 75% 之间的随机数,这可以用 Jquery 实现吗?到目前为止,我已经尝试了所有我能想到和研究的方法,但无济于事..

有人可以帮忙吗?

我对 JQuery 不是很好,因此非常感谢您的帮助

我正在尝试使用插件 Instafeed 随机化带有特定主题标签的用户帐户中的图像大小,因此我完全拥有:

我需要 .insta-box 在每次加载图像时随机生成一个尺寸...

<style>
.insta-box {float:center;clear:both;margin:0px;padding:0%;max-height:50%;overflow:hidden;border-width:0;display:inline-block;}</style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script>
    (function ($) {
        var userId = "UID";
        var accessToken = "Token";
        var numDisplay = "9";
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            cache: false,
url: "https://api.instagram.com/v1/users/"+userId+"/media/recent/?    access_token="+accessToken,
            success: function(data) {
                for (var i = 0; i < numDisplay; i++) {                      
        var rand = Math.floor(Math.random()*10)+10+"%";
            $(".insta-box").css('width', rand);
$(".insta").append("<div class='insta-box'><a target='_blank' href='"+data.data[i].link+"'>
    <img class='instagram-image' src='" + data.data[i].images.low_resolution.url+"'width='auto'/><a></div>"); 
            $(".insta").css('width', '100%');
            }    

            }
        });
    })(jQuery);
    </script>

最佳答案

给你,宽度在窗口宽度的 25% 到 75% 之间: DEMO

var rand=Math.floor(Math.random()*75)+25;
var width=Math.round(($(window).width()/100)*rand);
$('img').width(width);

更新:

只需将此行 $(".insta-box").css('width', rand); 替换为以下内容

 $(".insta-box").eq(i).css('width', rand);

关于jquery - 使用 Jquery 随机设置图像的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710478/

相关文章:

java - 在绘制方法中设置随机颜色

java - 为什么 java.util.Random 使用掩码?

jquery - 如何在树中查找元素和 sibling

javascript - 如何按值将数组分配给另一个变量javascript

关于 if 语句和位置移动的 jQuery 问题

javascript - Div 出现在不同 div 的悬停上

c++ - Boost 中的 Gamma 分布

javascript - 只有第一个随机引用淡入

html - Safari 上 Bootstrap 容器内的全宽背景

html - 自定义字体(字体)在发布时不起作用