我有一张气球的照片。我需要它随机地在我的页面上飞来飞去(它有点小)。我只需要它在我的页面上半部分飞行。我找到了以下代码:
$("#Friends").animate({
top: "-=30px",
}, duration );但我不确定如何循环它,让它同时在x轴和y轴上运行。如果可以的话,谢谢!我确实启用了jQuery :)
发布于 2011-05-07 02:50:45
这样怎么样..。LIVE FIDDLE
HTML
<img src="http://www.birdsnways.com/imgs/blbd48rt.gif" id="picture" />CSS
#picture{
position:absolute;
}JS
doNextPoint();
function doNextPoint(){
var maxX = $(window).width() - $('#picture').width();
var newX = rand(0, maxX);
var maxY = ($(window).height()/2) - $('#picture').height();
var newY = rand(0, maxY);
var speed = rand (1000, 3000);
$('#picture').animate({
'top': newY + 'px',
'left': newX + 'px'
}, speed, function(){
doNextPoint();
});
}
function rand (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}发布于 2011-05-07 02:43:16
您可以将该代码放入一个命名函数中,然后将该函数添加为动画的回调参数,以便它在完成后再次调用自身。
var flying;
flying = function() {
$("#Friends").animate({
top: "-=30px", // you'll need to change this
},
duration,
flying
);
}
flying();正因为如此,它将继续向上飞行,因为动画总是设置为上升30px。你必须改变飞行函数来使运动随机化一点。为了更逼真,保存之前的运动,并只需将其更改一点(小加速度),这样它就不会有非常抖动的运动。
发布于 2011-05-07 02:40:56
要循环它:使用SetTimeout:https://developer.mozilla.org/en/window.setTimeout
对于x轴,使用CSS属性left:(top:将获得y轴)
https://stackoverflow.com/questions/5915622
复制相似问题