Vikram Vikram - 10 months ago 40
CSS Question

How to animate a Div from left to current position

Guys I am trying to move a car from left to the current position of the DIV.

Here is the JSFiddle



var pos = $('#parentx').offset() - ('.car1').offset() ;

$(".car1").animate({left: pos}, 2000);

}, 1000);


You have to set the car to some default position (just change the left value in css) and then check for the left offset of the .car1 parent and animate the left to that value.

function moveCar() {
    var newPos = $(".car1").parent().offset().left;
    $('.car1').animate({left: newPos + "px"}, 2000);

See fiddle