slideToggle() can detect SlideUp or SlideDown?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 09:35:56

问题


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 ?


回答1:


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




回答2:


What if you check the state of the slide and make than the function?

$("#divName").slideToggle(function(){
    var check=$(this).is(":hidden");

    if(check == true)
    {
     //do something
    }
});



回答3:


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.



来源:https://stackoverflow.com/questions/19657230/slidetoggle-can-detect-slideup-or-slidedown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!