我寫了這個代碼:3個烏雲飛過我的網站。
問題是:我可以如何縮短? (例如:在一個函數或類似的東西)jQuery - 如何不重複的代碼?
var x1 = 1500-Math.random()*2000;
function animateDivers1() {
var y1 = 500-Math.random()*600;
$('.section1 .cloud1').css('left', x1);
$('.section1 .cloud1').css('top', y1);
x1 = -400;
var time = 15000+Math.random()*10000;
$('.section1 .cloud1').animate({
left: x1+2000
}, time, "linear", animateDivers1
)}
animateDivers1();
var x2 = 1500-Math.random()*2000;
function animateDivers2() {
var y2 = 500-Math.random()*600;
$('.section1 .cloud2').css('left', x2);
$('.section1 .cloud2').css('top', y2);
x2 = -400;
var time = 15000+Math.random()*10000;
$('.section1 .cloud2').animate({
left: x2+2000
}, time, "linear", animateDivers2
)}
animateDivers2();
var x3 = 1500-Math.random()*2000;
function animateDivers3() {
var y3 = 500-Math.random()*600;
$('.section1 .cloud3').css('left', x3);
$('.section1 .cloud3').css('top', y3);
x3 = -400;
var time = 15000+Math.random()*10000;
$('.section1 .cloud3').animate({
left: x3+2000
}, time, "linear", animateDivers3
)}
animateDivers3();
,當然我想添加更多 - 但是複製這個代碼看起來傻
你爲什麼不只是通過'雲迭代.section1?' –