title: How do I use jquery's animate method to make a div move back and forth
body: <p>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. </p>
  
  <p>Here is the script that I am using:</p>
  
  <pre><code>  &lt;script type="text/javascript"&gt;
      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);
  &lt;/script&gt; 
  </code></pre>