A very simple solution:
$(function() { $('#div').ToggleSlide(); }); $.fn.ToggleSlide = function() { return this.each(function() { $(this).css('position', 'absolute'); if(parseInt($(this).css('right')) < 0) { $(this).animate({ 'right' : '-100px' }, 1000, function() { $(this).css('position', 'relative'); }); } else { $(this).animate({ 'right' : '0px' }, 1000, function() { $(this).css('position', 'relative'); }); } }); });
What we do here: When we call the function, we set the position of the position to โabsoluteโ so that we can easily animate it. We check if the element has a negative โrightโ (already moved to the right), if true, we brighten back to 0 (move from right to left), otherwise we animate โdirectlyโ (move from left to right). After the animation is completed, we set the position of the "relative" position so that we can use its size.
source share