我一直在研究这个功能,我有5个圆圈,然后当点击一个按钮时,它们按顺序一次向下移动一页。我试图让它们以随机顺序排列。 (如果不是随机的,至少某些东西不能使它们按顺序排列)
JsFiddle: https://jsfiddle.net/n12zj90p/2/
HTML:
<div class="container">
<button>CLICK</button>
<div class="circle circle-1"></div>
<div class="circle circle-2"></div>
<div class="circle circle-3"></div>
<div class="circle circle-4"></div>
<div class="circle circle-5"></div>
</div>
CSS:
.circle{
width: 40px;
height: 40px;
position: absolute;
background: red;
border-radius: 100%;
}
.circle-1{ top: 10em; left: 10em; }
.circle-2{ top: 20em; left: 20em; }
.circle-3{ top: 30em; left: 30em; }
.circle-4{ top: 40em; left: 40em; }
.circle-5{ top: 50em; left: 50em; }
button{ padding: 10px 50px; display: table; margin: auto; }
JQUERY:
$(document).ready(function(){
var $c = $(".circle");
$("button").click(function(){
$c.each(function(i, elem){
$(elem).delay(i * 500).animate({top:"300em"}, { duration: 2000, complete: function(){
//just something to do after the animation has been completed, you can disregaurd this area
}
});//animate end
});//each end
});
});
答案 0 :(得分:3)
var $c = $(".circle");
是一个数组。所以你只需要在它上面each
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle($c).each(...
答案 1 :(得分:1)
不确定我是否值得赞扬,因为这是Pointy的主意,但这里的代码非常简单:
$(document).ready(function(){
var $c = $(".circle");
$("button").click(function(){
$c.each(function(i, elem){
var rando_time = Math.floor((Math.random() * 600) + 1);
$(elem).delay(i * rando_time).animate({top:"300em"}, { duration: 2000, complete: function(){
//just something to do after the animation has been completed, you can disregaurd this area
}
});//animate end
});//each end
});
});