【发布时间】:2015-02-17 06:55:10
【问题描述】:
我想要一个生成一个从 0.1 到 1 的随机数的变量。在一个函数中,我想多次引用该变量,每个引用都应该生成一个唯一的数字……如果该漫游有意义的话。有些东西一眼就能看出来,但通常是用 C 或其他语言编写的。我在 javascript 中需要这个。
var randoms = Math.floor(Math.random() * 1) + .15;
我这里有这段代码,我在同一个函数中引用它大约 15 次。 每次我都希望数字不同。就目前而言,这会生成一个唯一编号,然后会被使用 15 次。
我知道它一定很简单,因为它是一个简单的概念,但我无法完全理解它。
一些指导会很棒。
也许一些上下文会有所帮助。 在这里,我有一组条形变量,在时间轴上的某个点我需要它们来触发宽度动画,但时间速率不同。
下面的 var randoms 需要针对所有 bar 元素调用的每个实例进行更改。
var bar1 = $('#bar1'),
bar2 = $('#bar2'),
bar3 = $('#bar3'),
bar4 = $('#bar4'),
bar5 = $('#bar5'),
bar6 = $('#bar6'),
bar7 = $('#bar7'),
bar8 = $('#bar8'),
bar9 = $('#bar9'),
bar10 = $('#bar10'),
bar11 = $('#bar11'),
bar12 = $('#bar12'),
bar13 = $('#bar13'),
bar14 = $('#bar14'),
bar15 = $('#bar15')
icon = $('.icon'),
randoms = Math.floor(Math.random() * 1) + .15;
init = new TimelineMax();
init.add ("chart" , "+=2")
init.to(icon_1, 0.5, {alpha: '1'})
.to(icon_2, 0.5, {alpha: '1'})
.to(icon_3, 0.5, {alpha: '1'})
.to(icon_4, 0.5, {alpha: '1'})
.to(bar1, randoms, {width: '100%'},'chart')
.to(bar2, randoms, {width: '100%'},'chart')
.to(bar3, randoms, {width: '75%'},'chart')
.to(bar4, randoms, {width: '90%'},'chart')
.to(bar5, randoms, {width: '85%'},'chart')
.to(bar6, randoms, {width: '100%'},'chart')
.to(bar7, randoms, {width: '75%'},'chart')
.to(bar8, randoms, {width: '90%'},'chart')
.to(bar9, randoms, {width: '90%'},'chart')
.to(bar10, randoms, {width: '70%'},'chart')
.to(bar11, randoms, {width: '50%'},'chart')
.to(bar12, randoms, {width: '85%'},'chart')
.to(bar13, randoms, {width: '80%'},'chart')
.to(bar14, randoms, {width: '90%'},'chart')
.to(bar15, randoms, {width: '90%'},'chart');
【问题讨论】:
标签: javascript jquery random