jquery toggle slideUp/slideDown

后端 未结 3 572
孤独总比滥情好
孤独总比滥情好 2021-01-17 22:45

I\'ve got a div#items and if it\'s clicked then div#choices slideDown(). If it\'s clicked again then div#choices should slideUp(); How can I test if choices is down or up al

相关标签:
3条回答
  • 2021-01-17 23:05

    Try this -


    HTML:

    <a href="#demo" class="nav-toggle">Show</a>
    <div id="demo" style="display:none;">
    <p>short info</p>
    </div>
    


    JQUERY SCRIPT:

    $('.nav-toggle').click(function(){
       var content_id = $(this).attr('href');               
       var toggle_switch = $(this); 
         $(content_id).toggle(function(){
        if($(this).css('display')=='none'){
              toggle_switch.html('Show');
        }else{
          toggle_switch.html('Hide');
            }
        });  
    });
    
    0 讨论(0)
  • 2021-01-17 23:21

    Use .slideToggle()

    0 讨论(0)
  • 2021-01-17 23:22

    As Marimuthu's answer points out, you can use slideToggle() to slide up if the div is already open, or down if it's closed.

    You can also check the display property to find out if it's currently visible or not. From the jQuery docs for slideUp().

    Once the height reaches 0, the display style property is set to none to ensure that the element no longer affects the layout of the page.

    Knowing this, you can check to see if it is set to "none":

    var $choices = $('div#choices');
    if ($choices.css("display") == "none")
        $choices.slideDown();
    else
        $choices.slideUp();
    
    0 讨论(0)
提交回复
热议问题