Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swing效果jQuery动画背景_Jquery_Swing_Animation_Background - Fatal编程技术网

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
            );
        }