最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make infinite movement of a div? - Stack Overflow

programmeradmin2浏览0评论

I am trying to make a webpage that uses a moving div that slowly moves to the left. There is also another div that moves on top of it to the right that gives a 3D effect (but that's beside the point).

What I am trying to do right now has made a 7000px wide div that slowly moves to the left (animating the "right" property of CSS with jQuery animate()), but after it's all moved by, the animation ends.

Is there a way to make the div an infinite width or at least cause it to go back to the beginning (much like a grocery store checkout moving belt thing) so the animation never stops?

I am thinking this requires a few seperate divs, each once it reaches its end, goes back, but I can't figure out how to do that as I am a beginner jQuery developer.

I am trying to make a webpage that uses a moving div that slowly moves to the left. There is also another div that moves on top of it to the right that gives a 3D effect (but that's beside the point).

What I am trying to do right now has made a 7000px wide div that slowly moves to the left (animating the "right" property of CSS with jQuery animate()), but after it's all moved by, the animation ends.

Is there a way to make the div an infinite width or at least cause it to go back to the beginning (much like a grocery store checkout moving belt thing) so the animation never stops?

I am thinking this requires a few seperate divs, each once it reaches its end, goes back, but I can't figure out how to do that as I am a beginner jQuery developer.

Share Improve this question asked Jun 15, 2009 at 22:32 Brandon WangBrandon Wang 3,9717 gold badges32 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

UPDATE

Taking in account your example at http://nth.brandonwang/stuhf2/simpleindex.html you can fix it just by adding a callback at the end, your script must be like this:

$(document).ready(function() {
    animateRight ();
    animateLeft ();
});

function animateRight ()
{
    $('#lightright').css ("left", "100%");
    $('#lightright').animate({left:'0%'},1500, "", animateRight);
}

function animateLeft ()
{
    $('#lightleft').css ("right", "100%");
    $('#lightleft').animate({right:'0%'},1600, animateLeft);
}

Basically we just reset the animated css property and start de animation effect, the animation duration is faster in this code just to help to see the effect.

Hope that it helps you

You could look into using the jQuery marquee plugin:

http://remysharp./demo/marquee.html
http://remysharp./2008/09/10/the-silky-smooth-marquee/

Are you trying to implement parallax in your page? There are a couple of plugins for handling this.

http://plugins.jquery./project/jparallax http://plugins.jquery./project/jquery-parallax http://en.wikipedia/wiki/Parallax

发布评论

评论列表(0)

  1. 暂无评论