This happens because you are setting left to 400 always, if you set left to 800 for the second one than it will work
http://jsfiddle.net/oesw6onk/2/
$(document).ready(function () {
$("button").click(function () {
$("div").animate({
left: '400',
opacity: '1',
}, function () {
$("div").animate({
left: '800',
opacity: '0',
})
});
});
});
I put the second animation in the complete function of the first as well.
OR
you can use the increment
http://jsfiddle.net/oesw6onk/3/
$(document).ready(function () {
$("button").click(function () {
$("div").animate({
left: '+=400',
opacity: '1',
}, function () {
$("div").animate({
left: '+=400',
opacity: '0',
})
});
});
});
You might want to set the left to 0 again after the final animation for this version as you can see the box will keep 400 pixel to the right every time you click the button.
Example of the left reset http://jsfiddle.net/oesw6onk/4/
CSS option
http://jsfiddle.net/oesw6onk/6/
@-webkit-keyframes NAME-YOUR-ANIMATION {
0% {
opacity: 0;
left: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
left: 800px;
}
}
@-moz-keyframes NAME-YOUR-ANIMATION {
0% {
opacity: 0;
left: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
left: 800px;
}
}
@-o-keyframes NAME-YOUR-ANIMATION {
0% {
opacity: 0;
left: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
left: 800px;
}
}
@keyframes NAME-YOUR-ANIMATION {
0% {
opacity: 0;
left: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
left: 800px;
}
}
div {
-webkit-animation: NAME-YOUR-ANIMATION 2s infinite;
-moz-animation: NAME-YOUR-ANIMATION 2s infinite;
-o-animation: NAME-YOUR-ANIMATION 2s infinite;
animation: NAME-YOUR-ANIMATION 2s infinite;
}