-1
    <script type="text/javascript">
        $(function (){
            $(".links").click(function(){ 
            $('.slider').stop(true,false).animate({right:  "0" }, 800, 'easeOutQuint' ); },
        function(){ 
            $(".slider").stop(true,false).animate({right: "-200" }, 800, 'easeInQuint' ); },1000);
        });

   </script>

I am building a little slider on my website. The slider position is right: -200. It slides to position right:0 I want to animate it back to position right: -200 after clicking anywhere else on the page.

I tried all the ways which failed. toggle(slide) works good but doesn't looks good.

Frostbourn
  • 330
  • 3
  • 21

3 Answers3

0

Bind click on all document, stop current animation, run new animation.
$(document).click(function () { $('.slider').stop(true).animate({right: -200}, 500); });

sergolius
  • 428
  • 3
  • 8
0

well, here you go

$(document).click(function(e){
   e.preventDefault();

   if($(e.target).closest("your_slider_selector").length) return;

   //here you can do what you want
});
Smile0ff
  • 788
  • 7
  • 18
0

Store the CSS value in a variable before you animate the slider:

var right = $('.slider').css("right");

And then you can just use the variable:

$('.slider').stop(true).animate({right: right}, 800);

Here an example: http://jsfiddle.net/ctdjkrLx/2/

Dennis98
  • 139
  • 1
  • 12