Can I check if Bootstrap Modal Shown / Hidden?

前端 未结 11 1410
走了就别回头了
走了就别回头了 2020-12-05 12:29

Can I check if Bootstrap Modal currently Shown / Hidden Programatically?

Like bool a = if(\"#myModal\").shown(); ?

I need true/false

相关标签:
11条回答
  • 2020-12-05 12:58

    When modal hide? we check like this :

    $('.yourmodal').on('hidden.bs.modal', function () {
        // do something here
    })
    
    0 讨论(0)
  • 2020-12-05 12:59

    In offical way:

    > ($("element").data('bs.modal') || {})._isShown    // Bootstrap 4
    > ($("element").data('bs.modal') || {}).isShown     // Bootstrap <= 3
    

    {} is used to avoid the case that modal is not opened yet (it return undefined). You can also assign it equal {isShown: false} to keep it's more make sense.

    0 讨论(0)
  • 2020-12-05 13:01

    Here's some custom modal code that gives the modal states more explicitly named classes:

    $('.modal').on('show.bs.modal', function(e)
    {
        e.currentTarget.classList.add("modal-fading-in");
        e.currentTarget.classList.remove("modal-fading-out");
        e.currentTarget.classList.remove("modal-hidden");
        e.currentTarget.classList.remove("modal-visible");
    });
    
    $('.modal').on('hide.bs.modal', function(e)
    {
        e.currentTarget.classList.add("modal-fading-out");
        e.currentTarget.classList.remove("modal-fading-in");
        e.currentTarget.classList.remove("modal-hidden");
        e.currentTarget.classList.remove("modal-visible");
    });
    
    $('.modal').on('hidden.bs.modal', function(e)
    {
        e.currentTarget.classList.add("modal-hidden");
        e.currentTarget.classList.remove("modal-fading-in");
        e.currentTarget.classList.remove("modal-fading-out");
        e.currentTarget.classList.remove("modal-visible");
    });
    
    $('.modal').on('shown.bs.modal', function(e)
    {
        e.currentTarget.classList.add("modal-visible");
        e.currentTarget.classList.remove("modal-fading-in");
        e.currentTarget.classList.remove("modal-fading-out");
        e.currentTarget.classList.remove("modal-hidden");
    });
    

    You can then easily target the modal's various states with both JS and CSS.

    JS example:

    if (document.getElementById('myModal').hasClass('modal-fading-in'))
    {
        console.log("The modal is currently fading in. Please wait.");
    }
    

    CSS example:

    .modal-fading-out, .modal-hidden
    {
        opacity: 0.5;
    }
    
    0 讨论(0)
  • 2020-12-05 13:08

    Use hasClass('in'). It will return true if modal is in OPEN state.

    E.g:

    if($('.modal').hasClass('in')){
       //Do something here
    }
    
    0 讨论(0)
  • 2020-12-05 13:13

    For me this works

     
    if($("#myModal").css("display") !='none' && $("#myModal").css("visibility") != 'hidden')

    alert("modal shown");

    0 讨论(0)
  • 2020-12-05 13:14

    All Bootstrap versions:

    var isShown = $('.modal').hasClass('in') || $('.modal').hasClass('show')
    

    To just close it independent of state and version:

    $('.modal button.close').click()
    

    more info

    Bootstrap 3 and before

    var isShown = $('.modal').hasClass('in')
    

    Bootstrap 4

    var isShown = $('.modal').hasClass('show')
    
    0 讨论(0)
提交回复
热议问题