2013-07-24 115 views
0

我有一些将对象移动到轨道上的小脚本,我想随机化对象的起始位置。随机化动画对象的位置

这里就是我说的: http://jsfiddle.net/W69s6/1018/

HTML

<div class="orbit"> 
    <div class="circle"></div> 
</div> 

CSS

.orbit 
{ 
    height: 200px; 
    width: 200px; 
    background: rgba(0,0,0,0.2); 
} 

.circle 
{ 
    height: 50px; 
    width: 50px; 
    background: #00ff00; 
    position: absolute; 
} 

JS

$(document).ready(function() { 
    orbit('.circle', 0, 0.01, 100, 75, 75); 
}); 

function orbit(obj, t, ts, r, x, y) 
{ 
    t += ts; 

    var top = Math.floor(y - (r * Math.sin(t))); 
    var left = Math.floor(x - (r * Math.cos(t))); 

    $(obj).animate({ 
     top: top, 
     left: left, 
    }, 1, function() 
    { 
     orbit(obj, t, ts, r, x, y); 
    }); 
} 

是一种你擅长数学并知道如何改变它?

+0

随机化开始圈的位置? –

回答

1

更改代码的第一部分:

$(document).ready(function() { 
    orbit('.circle', Math.random()*100, 0.01, 100, 75, 75); 
}); 
+2

http://jsfiddle.net/LDsPJ/他的答案的小提琴。工程很好,是最简单的。 – defaultNINJA

+0

你不想应用'Math.random()'最后两个参数,因为那些是开始的X和Y值? – Jnatalzia

+1

因为X和Y参数会占据轨道位置,而不是圆形。 – mikach