Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slideToggle() can detect SlideUp or SlideDown?

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 ?

like image 672
Venzentx Avatar asked Oct 29 '13 11:10

Venzentx


People also ask

What does slideToggle do?

slideToggle() method animates the height of the matched elements. This causes lower parts of the page to slide up or down, appearing to reveal or conceal the items. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed.

How do you toggle slideUp and slideDown in jquery?

The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.


2 Answers

You can always keep a flag since there is no method in pure jQuery. Assuming #divName is not visible,

var toggle_flag = 0;

$('#divName').click(function () {

   $('#myelement').slideToggle();

   if(toggle_flag==0){
      //code for slide down
      toggle_flag=1;
   }else{
      //code for slide up
      toggle_flag=0;
   }

});

If #divName is already visible you can initiate with var toggle_flag=1; and use this code.

like image 141
Janaka Dombawela Avatar answered Sep 24 '22 11:09

Janaka Dombawela


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});.

like image 33
Owen C. Jones Avatar answered Sep 23 '22 11:09

Owen C. Jones