Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jquery's animate method to make a div move back and forth

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> 
like image 943
user798774 Avatar asked Feb 25 '12 08:02

user798774


People also ask

What is the correct syntax of animate () method?

The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);

Can the animate () method be used to animate any CSS property?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

What does an easing do?

Easing out causes the animation to start more quickly than linear ones, and it also has deceleration at the end. Easing out is typically the best for user interface work, because the fast start gives your animations a feeling of responsiveness, while still allowing for a natural slowdown at the end.

How you can use jQuery to animate a flash to the button?

The solution to Jquery To Animate A Flash To The Button Selected will be demonstrated using examples in this article. $("#someElement"). fadeOut(100). fadeIn(100).


3 Answers

Are you trying to do this? http://jsfiddle.net/vwH6R/2/

setInterval(function(){
    $("div").stop(true,true).animate({left: 300}, 1000, 
          function(){ $(this).stop(true,true).animate({left: 0}, 1000); 
    });
}, 2000);​
like image 64
Cheery Avatar answered Sep 26 '22 23:09

Cheery


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/

like image 22
Matt Ball Avatar answered Sep 26 '22 23:09

Matt Ball


Try this fiddle

HTML

<div class="myDiv">A Div</div>​

JS

$('.myDiv').animate({
   'margin-left':'50px'
}, 500, function(e){
$('.myDiv').animate({
    'margin-left':'0px'
}, 500)});​
like image 31
The Alpha Avatar answered Sep 22 '22 23:09

The Alpha