我编写了一个代码,用于将四个div移动到由随机数创建的任意位置的简单动画。我只想重复同样的过程10次。我使用了回调函数的概念,但它没有用。我使用了for循环,但是该过程仍然只执行一次。我怎样才能多次重复一个过程?
$(document).ready(function() {$( init )});
var xRandom;
var yRandom;
function createNumbers(callback) {
xRandom=200+10*Math.floor(Math.random()*9);
yRandom=200+10*Math.floor(Math.random()*9);
callback();
};
function init() {
for (var i=0;i<10;i++) {
createNumbers(process);
}
}
function process() {
var w=150;
var h=150;
$('.number').on("click",function() {
$(this).animate({
left : xRandom,
top : yRandom,
width : w,
height :h,
},1000);
});
}
body {
margin: 30px;
font-family: "Georgia", serif;
line-height: 1.8em;
color: #333;
}
#ejer {
position :relative;
width : 800px;
height : 600px;
background: lightgrey;
font-family: "Georgia", serif;
border: 2px solid grey;
z-index: 2;
}
#div1 {
position :absolute;
top : 50px;
left :50px;
z-index: 1;
}
#div2 {
position :absolute;
top : 50px;
left :150px;
}
#div3 {
position :absolute;
top : 50px;
left :250px;
}
#div4 {
position :absolute;
top : 50px;
left :350px;
}
.number {
position :absolute;
top : 50px;
left :50px;
width : 100px;
height : 50px;
background: yellow;
border: 2px solid grey;
border-radius: 10px;
text-align: center;
vertical-align: middle;
display: table-cell;
font: 30px Verdana, sans-serif;
z-index: 2;
}
<head>
<title>A simple loop example</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<body>
<div id="ejer" >
<div id="div1" class="number">div1</div>
<div id="div2" class="number">div2</div>
<div id="div3" class="number">div3</div>
<div id="div4" class="number">div4</div>
</div>
</body>
</head>
答案 0 :(得分:1)
我相信你在点击它时想要移动div 10次。如果是这种情况,则问题在于循环的位置以及添加动画的方式。试试这个:
$(document).ready(function() {$( init )});
var xRandom;
var yRandom;
function createNumbers() {
xRandom=200+10*Math.floor(Math.random()*9);
yRandom=200+10*Math.floor(Math.random()*9);
return [xRandom, yRandom]
};
function init() {
var numbers = [];
for (var i=0;i<10;i++) {
numbers.push(createNumbers());
}
process(numbers);
}
function process(numbers) {
var w=150;
var h=150;
$('.number').on("click",function() {
for(var i = 0;i < numbers.length; i++) {
$(this).animate({
left : numbers[i][0],
top : numbers[i][1],
width : w,
height :h,
}, {
duration: 1000,
queue: true
});
}
});
}
body {
margin: 30px;
font-family: "Georgia", serif;
line-height: 1.8em;
color: #333;
}
#ejer {
position :relative;
width : 800px;
height : 600px;
background: lightgrey;
font-family: "Georgia", serif;
border: 2px solid grey;
z-index: 2;
}
#div1 {
position :absolute;
top : 50px;
left :50px;
z-index: 1;
}
#div2 {
position :absolute;
top : 50px;
left :150px;
}
#div3 {
position :absolute;
top : 50px;
left :250px;
}
#div4 {
position :absolute;
top : 50px;
left :350px;
}
.number {
position :absolute;
top : 50px;
left :50px;
width : 100px;
height : 50px;
background: yellow;
border: 2px solid grey;
border-radius: 10px;
text-align: center;
vertical-align: middle;
display: table-cell;
font: 30px Verdana, sans-serif;
z-index: 2;
}
<head>
<title>A simple loop example</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<body>
<div id="ejer" >
<div id="div1" class="number">div1</div>
<div id="div2" class="number">div2</div>
<div id="div3" class="number">div3</div>
<div id="div4" class="number">div4</div>
</div>
</body>
</head>
我所做的重大改变是
答案 1 :(得分:0)
你可以实现类似游戏循环的东西。
^(([1-4][0-9]{0,3})|([1-9][0-9]{0,2})|(5000))$
您要在重复的函数末尾调用requestAnimationaFrame。