简体   繁体   English

在页面上前后移动div

[英]Move a div back and forth across page

I'm trying to get a div to move from one end of the screen to the other on a loop. 我正在尝试使div从屏幕的一端循环移动到另一端。 My javascript currently only attempts to move the div left but doesn't work. 我的JavaScript当前仅尝试将div左移,但不起作用。

    var func = function() {
       $("#bob").animate({"left": "-40px"}, 1000, function() { 
          $(this).animate({"left": "40px"}, 1000) 
       })

        setTimeout(func, 2000);
    }

Here is my jsfiddle: 这是我的jsfiddle:

http://jsfiddle.net/zsal/N48Eg/1/ http://jsfiddle.net/zsal/N48Eg/1/

Name your functions, and then use one as the completion callback in the other: 命名您的函数,然后将其中一个用作另一个的完成回调:

function goLeft() {
    $('#bob').animate({'left': '-40px'}, 1000, goRight);
}

function goRight() {
    $('#bob').animate({'left': '40px'}, 1000, goLeft);
}

goLeft();

So, when it's done going left, it should go right. 因此,完成操作后,它应该向右走。 When it's done going right, it should go left. 完成后,它应该向左走。

Disclaimer: Untested 免责声明:未经测试

PS You're missing jQuery in your Fiddle. PS:您的小提琴中缺少jQuery。

Your current fiddle doesn't work because you didn't include jQuery (from the Frameworks & Extensions drop-down on the left) and because you define the func() function but never actually call it. 您当前的小提琴无法正常工作,因为您没有包括jQuery(从左侧的Frameworks&Extensions下拉列表中),并且因为您定义了func()函数,但从未真正调用它。 Fix those two things and it will work as shown here: http://jsfiddle.net/N48Eg/8/ 修正这两个问题,它将如以下所示正常工作: http : //jsfiddle.net/N48Eg/8/

Note, however, that your animation code is more complicated than it needs to be. 但是请注意,动画代码比需要的复杂。 Multiple animations on the same element will be queued automatically by jQuery, so you don't need to use a callback on the first one to start the second. jQuery将自动将同一元素上的多个动画排入队列,因此您不需要在第一个上使用回调来启动第二个。 And you can supply func as the callback on the second and avoid the setTimeout() completely: 而且,您可以在第二个事件中提供func作为回调,并完全避免使用setTimeout()

var func = function() {
    $("#bob").animate({"left": "-40px"}, 1000)
             .animate({"left": "40px"}, 1000, func);
}

func();

Demo: http://jsfiddle.net/N48Eg/18/ 演示: http//jsfiddle.net/N48Eg/18/

You just haven't called your function... 您只是没有调用函数...

var func = function() {
    $("#bob").animate({"left": "-40px"}, 1000, function() { 
        $(this).animate({"left": "40px"}, 1000) 
    })
 setTimeout(func, 2000); // added back
}
func();

Your updated fiddle 您更新的小提琴

First of, if you want the motion to continue, you need to use setInterval instead of setTimeout 首先,如果要继续运动,则需要使用setInterval而不是setTimeout

Also you need to either move the setInterval out of the scope of func , or call func to start the animation 另外,您需要将setInterval移出func的范围之外,或者调用func来启动动画。

lots of problems. 很多问题。 Heres a fiddle with a solution. 这是一个解决办法的小提琴。

you also forgot to position:relative your absolutely positioned elements container. 您还忘记position:relative绝对定位的元素容器。

JSFIDDLE JSFIDDLE

you weren't calling your function for starters 您没有为初学者调用函数

i made a new one called moveBob() 我做了一个新的叫做moveBob()

Here's the pure CSS version. 这是纯CSS版本。 http://jsfiddle.net/zDSRd/ http://jsfiddle.net/zDSRd/

#bob
{
    position:absolute;

    animation: fly 2s linear 0s alternate infinite;
    -webkit-animation: fly 2s linear 0s alternate infinite;
    -moz-animation: fly 2s linear 0s alternate infinite;
    -o-animation: fly 2s linear 0s alternate infinite;
    -ms-animation: fly 2s linear 0s alternate infinite;
}
@keyframes fly {
    from { transform: translateX(0px); }
    to { transform: translateX(500px); }
}
@-webkit-keyframes fly {
    from { -webkit-transform: translateX(0px); }
    to { -webkit-transform: translateX(500px); }
}
@-moz-keyframes fly {
    from { -moz-transform: translateX(0px); }
    to { -moz-transform: translateX(500px); }
}
@-o-keyframes fly {
    from { -o-transform: translateX(0px); }
    to { -o-transform: translateX(500px); }
}
@-ms-keyframes fly {
    from { -ms-transform: translateX(0px); }
    to { -ms-transform: translateX(500px); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM