我有附接到所述主體標籤的固定的背景圖像,該圖像是2048像素由2048像素並且頂部的中心位置:移動的背景圖像隨機和順利
body {
background: url(images/background.jpg) 50% 0% no-repeat fixed;
}
欲一些運動添加到背景,因此它平移整個圖像周圍,而觀衆在瀏覽網站
使用:https://github.com/brandonaaron/jquery-cssHooks/blob/master/bgpos.js正常化的背景位置屬性,我可以按如下動畫形象:
$('body').animate({
backgroundPositionX: '100%',
backgroundPositionY: '100%'
}, 10000, 'linear');
我想添加隨機移動,而不是在10秒內進入右下角。
使用設置的時間間隔我可以每隔10秒爲圖像製作動畫,但是如何確定背景位置百分比?當圖像移動它應該是相同的速度,只是隨機位置
var currX = 50;
var currY = 0;
$(function() {
animate();
window.setInterval(animate(), 10000);
});
function animate() {
var newPosX = currX - (Math.random() * 50);
var newPosY = currY - (Math.random() * 50);
$('body').animate({
backgroundPositionX: newPosX + '%',
backgroundPositionY: newPosY + '%'
}, 10000, 'linear');
}
編輯:提琴更好地描述了我想做的事:http://jsfiddle.net/97w7f3c8/4/
+1古樸典雅。 – arao6 2014-11-08 05:14:46
@ user3023421我做了一個小的校正,兩個座標的角度應該是一樣的,這樣速度就會不變。 – Cheery 2014-11-08 05:18:19