Animating on the right
property (as you already tried) and afterwards setting the left
property might work for you. I will only 'break' if you resize your window during animation, although after the animatin is finished everything will look ok again.
$(".box").click(function() {
var parentWidth = $(this).parent().width();
var newRight = parentWidth - $(this).width() - 10;
$(this).animate({right: newRight}, function() {
// On animation finish:
$(this).css('right', 'auto')
.css('left', '10px');
});
});
(Edit, after first comment):
JQuery.animate calculates the step-size at the beginning. So if you work with pixels that won't work well if your parent-div resizes during the animation. If you however work with percentages it will work. Following might be an quite ugly hack:
New CSS (give wrap a padding of the width of your box, and box an left margin of 100%)
.wrap {
height: 50px;
background: #aaa;
position:relative;
top:0;
left:0;
padding: 10px 110px 10px 10px;
}
.box {
width:100px;
height:50px;
background: red;
margin-left: 100%;
}
And animate the left-margin to 0% using javascript:
$(".box").click(function() {
$(this).animate({
marginLeft: '0%'
});
});
JSFiddle (I've given it a very slow speed, so you can try resizing the frame)