1

I am having trouble getting this animation to work.

$("#selectedTime").stop().animate({
    "margin-left": "-=470px"
},"fast", function() {
    $(this).css({ "margin-left": "620px"
})}).animate({
    "margin-left": "-=470px"
},"fast");

I am sliding the div to the left 470px, jumping to 620px and sliding another 470px to the left. $(this).css({ "marginLeft": "620px" does not seem to be working.

The initial margin-left is 150px. After running the script it is -790px. (150-470-470).

3
  • 1
    And what's the trouble you're having? Commented Jul 2, 2011 at 13:40
  • -=, did u try with simply -470px? Commented Jul 2, 2011 at 13:43
  • What other CSS is applied to #selectedTime? Commented Jul 2, 2011 at 13:47

1 Answer 1

1

To start, you've got syntax errors:

$("#selectedTime").stop().animate({
    "margin-left": "-=470px"
},"fast", function() {              // ↓↓↓ here 
    $(this).css({ "margin-left": "620px"}); 
}).animate({
    "margin-left": "-=470px"
},"fast");

$("#selectedTime")
    .stop()
    .animate({"margin-left": "-=470px"}, "fast")
    .animate({"margin-left": "620px"}, 0)
    .animate({"margin-left": "-=470px"}, "fast");

Demo: http://jsfiddle.net/mattball/j7rcr/

Sign up to request clarification or add additional context in comments.

2 Comments

Same problem. Still ending up at -790px.
Beautiful. I guess it makes sense that it should be an animation, I didn't think of setting the duration to 0 though. Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.