I have two functions:
function animateOpen(){
var originalPosition = $(this).css("left");
var distance = $(this).getHorzontalDistanceToCenter();
$(this).animate("left", "+="+distance);
$('#button').click( function (){
$(this).animate("left", originalPosition+"px");
});
}
function animateClose(){
$('#button').click();
}
I want to convert this code to the bottom form (to remove the dependency on button):
function animateClose(){
$(this).animate("left", originalPosition+"px");
}
function animateOpen(){
var originalPosition = $(this).css("left");
var distance = $(this).getHorzontalDistanceToCenter();
$(this).animate("left", "+="+distance);
}
The problem is, how does animateClose get the originalPosition? Can I somehow put it in $(this)?
You can save the original position using
$(this).data('original_left', my_value)And afterwards, get the saved value with
$(this).data('original_left')