slideToggle() can detect SlideUp or SlideDown?

Venzentx picture Venzentx · Oct 29, 2013 · Viewed 17.8k times · Source

I know hover() has default setting for handIn and handOut, but is there anyway I could detect slideUp and SlideDown inside SlideToggle?

Have code like this:

$("#divName").slideToggle(function(){//when slideUp, do something},
                          function(){//when slideDown, do something});

I tried this code but no luck, do you guys think this may sometimes make things a bit easier ?

Answer

Owen C. Jones picture Owen C. Jones · Oct 29, 2013

slideToggle doesn't allow for that as default, but it would be easy enough to write your own version. Something like this would work as you alternative handler:

$('#divTrigger').click(function () { // Or bind to any other event you like, or call manually

    var $t = $('#divToSlide');

    if ($t.is(':visible')) {
        $t.slideUp();
        // Other stuff to do on slideUp
    } else {
        $t.slideDown();
        // Other stuff to down on slideDown
    }
});

Equally, if you wanted actions to happen after slideUp or slideDown, you could put them into the callback, like this $.slideUp(300, function() {// Callback here});.