Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:40:33+00:00 2026-05-30T09:40:33+00:00

I want to make a div animate horizontally back and forth within a certain

  • 0

I want to make a div animate horizontally back and forth within a certain area. The problem I have right now is that I can’t figure out how to make my div move back after it has moved forward.

Here is the script that I am using:

  <script type="text/javascript">
    var animatethis = function (targetElement, speed) {
        $(targetElement).animate(
            {
                marginLeft: "+=250px"
            },
            {
                duration: speed,
                complete: function () {
                    animatethis(this, speed);
                    animatethis.stop();
                    $(targetElement).animate({ marginLeft: "-=250px" });
                }
            }
                    )
    };
    animatethis($('#q1'), 5000);
</script> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:40:34+00:00Added an answer on May 30, 2026 at 9:40 am

    Look in your browser console. animatethis does not return anything, which means that the line

    animatethis.stop();
    

    is always going to crash (something like “TypeError: Cannot call method ‘stop’ of undefined”). You’re also mixing up the order of the animations in the complete callback.

    Stop, breathe, and think about what your code above is actually doing.


    Okay, so after the marginLeft is increased, you want to decrease it. Then you want to start all over. Right? So:

    function animatethis(targetElement, speed) {
        $(targetElement).animate({ marginLeft: "+=250px"},
        {
            duration: speed,
            complete: function ()
            {
                targetElement.animate({ marginLeft: "-=250px" },
                {
                    duration: speed,
                    complete: function ()
                    {
                        animatethis(targetElement, speed);
                    }
                });
            }
        });
    };
    
    animatethis($('#q1'), 5000);
    

    There are lots of other ways to skin this cat, but this does work. http://jsfiddle.net/mattball/rKu6Y/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element, like this: <div id=navleft> </div> I want to make this
I have two divs that have 50% width each. I want to make it
I have two divs which I want to animate: <div id="character"> <div id="sprite"></div> </div>
I want to have a div that animates the currently active image out of
I have a problem with animate loop. There is an object i want to
I'm trying to make a div fade in/out that's within an each statement. The
I have 4 icons that, on mouse over, scroll a div using jQuery animate.
I have an example: http://jsfiddle.net/kamaci/uVQZc/3/ When I animate bigger blue div, I want to
I have style sheet with a class name changebackgroundcolor i want make change in
I want to make an etag that matches what Apache produces. How does apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.