Swing效果jQuery动画背景
我正在为一个网页制作一个自动设置动画的背景摆动,我不知道当背景回到原点时如何解决这个问题 这是我在其他一些网站上所做的代码:Swing效果jQuery动画背景,jquery,swing,animation,background,Jquery,Swing,Animation,Background,我正在为一个网页制作一个自动设置动画的背景摆动,我不知道当背景回到原点时如何解决这个问题 这是我在其他一些网站上所做的代码: function scrollbackground() { // decrease the offset by 1, or if its less than 1 increase it by the background height minus 1 // offset = (offset < 1) ? offset + (back
function scrollbackground() {
// decrease the offset by 1, or if its less than 1 increase it by the background height minus 1
// offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
offset = offset - 1;
// apply the background position
$('body').css("background-position", "50% " + offset + "px");
// call self to continue animation
setTimeout(function() {
scrollbackground();
}, 1000
);
}
这对于向一个方向移动非常有效。我想要的是,当背景到达特定偏移量时,将其恢复为0。您可以将其作为参数发送
function scrollbackground(limit) {
// decrease the offset by 1, or if its less than 1 increase it by the background height minus 1
// offset = (offset < 1) ? offset + (backgroundheight - 1) : offset - 1;
offset = offset<limit?offset - 1:offset + 1;
// apply the background position
$('body').css("background-position", "50% " + offset + "px");
// call self to continue animation
setTimeout(function() {
scrollbackground(limit);
}, 1000
);
}
试试这个
var backgroundImageWidth;
var moveLeft = true;
var offset = 0;
function scrollbackground() {
offset = moveLeft?offset-1:offset+1;
if(offset*-1 == backgroundImageWidth)
moveLeft = false;
else if(offset == 0)
moveLeft = true;
// apply the background position
$('body').css("background-position", "50% " + offset + "px");
// call self to continue animation
setTimeout(function() {
scrollbackground();
}, 1000
);
}