jQuery .show() a flex box

前端 未结 9 1289
南笙
南笙 2021-02-06 20:56

I have an element with a flexbox

    div#myFlexbox{ display:flex; }

after hide it and show it, it gets messed up.<

9条回答
  •  遇见更好的自我
    2021-02-06 21:41

    JQuery .show() doesn't know about your display: flex; (not flexbox). Instead try to add and remove a hiding class.

    CSS:

    #myFlexbox{
      display: flex;
    }
    
    .hide{
      display: none !important;
    }
    

    JS:

    $('#myFlexbox').addClass('hide');
    $('#myFlexbox').removeClass('hide');
    

    https://jsfiddle.net/p2msLqtt/

    Otherwise you have to execute your JS after the CSS is completely loaded and DOM is ready I guess - i.e. like:

    
      
        
      
      
        
        
      
    
    

    Updated the fiddle and set Javascript execution to domready - it's working:

    https://jsfiddle.net/p2msLqtt/1/

提交回复
热议问题