Javascript 以随机序列为元素设置动画
我尝试设置一些按钮的动画。应生成一个随机字符串,使动画按钮不总是在同一序列中 例如:Javascript 以随机序列为元素设置动画,javascript,jquery,animation,Javascript,Jquery,Animation,我尝试设置一些按钮的动画。应生成一个随机字符串,使动画按钮不总是在同一序列中 例如: text = 1423 第一个动画按钮是btn1然后在一秒钟之后btn4然后在一秒钟之后btn2然后在一秒钟之后btn3 按钮: <div> <button name="btn1" type="submit" id="btn1" value="1" style="" class="button"></button> <button name="btn2
text = 1423
第一个动画按钮是btn1
然后在一秒钟之后btn4
然后在一秒钟之后btn2
然后在一秒钟之后btn3
按钮:
<div>
<button name="btn1" type="submit" id="btn1" value="1" style="" class="button"></button>
<button name="btn2" type="submit" id="btn2" value="2" style="" class="button"></button>
</div>
<div>
<button name="btn3" type="submit" id="btn3" value="3" style="" class="button"></button>
<button name="btn4" type="submit" id="btn4" value="4" style="" class="button"></button>
</div>
javascript:
var textArray = [];
var text = "";
function makeid()
{
var possible = "1234";
for( var i=0; i < 4; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
makeid();
textArray = text.split("");
console.log(textArray);
function animate1() {
$('#btn' + textArray[0]).animate( { backgroundColor: 'red' }, 500);
}
function animate2() {
$('#btn' + textArray[1]).animate( { backgroundColor: 'red' }, 500);
}
function animate3() {
$('#btn' + textArray[2]).animate( { backgroundColor: 'red' }, 500);
}
function animate4() {
$('#btn' + textArray[3]).animate( { backgroundColor: 'red' }, 500);
}
window.setTimeout(animate1, 1000);
window.setTimeout(animate2, 2000);
window.setTimeout(animate3, 3000);
window.setTimeout(animate4, 4000);
var textArray=[];
var text=“”;
函数makeid()
{
var可能=“1234”;
对于(变量i=0;i<4;i++)
text+=可能的.charAt(Math.floor(Math.random()*可能的.length));
返回文本;
}
makeid();
textArray=text.split(“”);
log(textArray);
函数1(){
$(#btn'+textary[0]).animate({backgroundColor:'red'},500);
}
函数2(){
$(#btn'+textary[1]).animate({backgroundColor:'red'},500);
}
函数animate3(){
$(#btn'+textary[2]).animate({backgroundColor:'red'},500);
}
函数animate4(){
$(#btn'+textary[3]).animate({backgroundColor:'red'},500);
}
设置超时(动画1,1000);
设置超时(animate2,2000);
设置超时(动画3,3000);
setTimeout(动画4,4000);
您关于将可能文本
洗牌的问题,因为在您的函数中有可能重复相同的数字。比如:2,2,2,1或4,4,4,4等等
您可以使用此洗牌功能代替洗牌方法:
函数shuffleWord(word){
var-shuffledWord='';
var charIndex=0;
单词=单词分割(“”);
while(word.length>0){
charIndex=word.length*Math.random()您关于将可能文本
洗牌的问题,因为在您的函数中有可能重复相同的数字。例如:2,2,2,1或4,4,4,4等等
您可以使用此洗牌功能代替洗牌方法:
函数shuffleWord(word){
var-shuffledWord='';
var charIndex=0;
单词=单词分割(“”);
while(word.length>0){
charIndex=word.length*Math.random()除非使用jQuery.Color
插件,否则无法设置backgroundColor
动画
所有已设置动画的属性应设置为单个数值,
除非如下所述;大多数非数字属性不能
使用基本jQuery功能(例如,宽度、高度、,
或“左”可以设置动画,但不能设置背景色,除非
使用jQuery.Color插件)
创建随机顺序时允许重复ID。此外,代码可以简化
var textArray = [];
function makeid() {
var num;
var possible = "1234";
while(textArray.length < 4) {
num = possible.charAt(Math.floor(Math.random() * possible.length));
if (textArray.indexOf(num) === -1) {
textArray.push(num)
}
}
}
function animate(id, wait) {
setTimeout(function() {
$('#btn' + id).animate({ width: '200'}, 500);
}, wait);
}
makeid();
for (var i=0; i < textArray.length; i++) {
animate(textArray[i], i * 1000)
}
var textArray=[];
函数makeid(){
var-num;
var可能=“1234”;
while(textArray.length<4){
num=可能的.charAt(Math.floor(Math.random()*可能的.length));
if(textArray.indexOf(num)=-1){
textArray.push(num)
}
}
}
函数动画(id,等待){
setTimeout(函数(){
$('#btn'+id).animate({width:'200'},500);
},等等);
}
makeid();
对于(var i=0;i
除非使用jQuery.Color
插件,否则无法设置backgroundColor
动画
所有已设置动画的属性应设置为单个数值,
除非如下所述;大多数非数字属性不能
使用基本jQuery功能(例如,宽度、高度、,
或“左”可以设置动画,但不能设置背景色,除非
使用jQuery.Color插件)
创建随机顺序时允许重复ID。此外,代码可以简化
var textArray = [];
function makeid() {
var num;
var possible = "1234";
while(textArray.length < 4) {
num = possible.charAt(Math.floor(Math.random() * possible.length));
if (textArray.indexOf(num) === -1) {
textArray.push(num)
}
}
}
function animate(id, wait) {
setTimeout(function() {
$('#btn' + id).animate({ width: '200'}, 500);
}, wait);
}
makeid();
for (var i=0; i < textArray.length; i++) {
animate(textArray[i], i * 1000)
}
var textArray=[];
函数makeid(){
var-num;
var可能=“1234”;
while(textArray.length<4){
num=可能的.charAt(Math.floor(Math.random()*可能的.length));
if(textArray.indexOf(num)=-1){
textArray.push(num)
}
}
}
函数动画(id,等待){
setTimeout(函数(){
$('#btn'+id).animate({width:'200'},500);
},等等);
}
makeid();
对于(var i=0;i