Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery为position创建随机css值_Javascript_Jquery_Css_Random_Margin - Fatal编程技术网

Javascript 使用jQuery为position创建随机css值

Javascript 使用jQuery为position创建随机css值,javascript,jquery,css,random,margin,Javascript,Jquery,Css,Random,Margin,是否可以使用jQuery/Javascript在-100和600之间创建随机数,以用作CSS中元素的位置(上边距、左边距)值?确保这是可能的,用于此 请参见MDN中的此示例: // Returns a random integer between min and max // Using Math.round() will give you a non-uniform distribution! function getRandomInt(min, max) { return Math.fl

是否可以使用jQuery/Javascript在-100和600之间创建随机数,以用作CSS中元素的位置(上边距、左边距)值?

确保这是可能的,用于此

请参见MDN中的此示例:

// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}
然后,可以使用此函数为元素提供随机边距

例如:

//Randomize the position of all your elements, identified by a specific class
//In case you have several items. Either, don't use the each()
$('.randomElement').each(function(){
    $(this).css({'marginTop' : getRandomInt(-100,600), 'marginTop' : getRandomInt(-100,600)});
});
试试这个

var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
当然是。 您需要使用random()函数。 然后使用jQuery设置要更改的属性。 详情如下:

var rand = Math.floor(Math.random() * (max - min + 1) + min);
$("#someid").attr("margin-top",rand);

你还不明白什么?你是在问如何选择一个随机数吗?如何将随机数乘以正确的范围?如何设置CSS属性?我对Javascript了解不够,我可以修改脚本,但很少能编写自己的脚本。我不知道是否有可能用
Math.floor((Math.random()*10)+1)生成负数我读过几个关于为css生成随机值的主题,但它们是关于颜色和其他一些非数字值的,我不知道如何在我的例子中使用。我问如何在正值和负值之间生成随机数,并将其插入(应用)css。@Kostsei我的解决方案有什么问题吗?非常感谢!对不起,但我仍然不明白如何安排这件事,在我看来这是一种正确的方式,但我对它了解不够,无法使它发挥作用: