我见过很多地方使用Math.floor()和Math.random()
如下
$('a.random-color').hover(function() { //mouseover
var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$(this).animate({
'color': col,
'paddingLeft': '20px'
},1000);
},function() { //mouseout
$(this).animate({
'color': original,
'paddingLeft': '0'
},500);
});
});
Run Code Online (Sandbox Code Playgroud)
为何使用Math.floor()和Math.random()?
pax*_*blo 10
Math.random 会给你一个介于0(含)和1(独占)之间的浮点数.
乘以256将得到0(包括)到256(不包括)范围内的数字,但仍然是浮点数.
取这个数字的最低点将给出一个0到255之间的整数(包括两者).
它是你需要构造RGB值的0到255之间的整数rgb(72,25,183).
| 归档时间: |
|
| 查看次数: |
7965 次 |
| 最近记录: |