jQuery .show() a flex box

前端 未结 9 1276
南笙
南笙 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:20

    The show() adds display:block as a default for div. You can change the display to flex using jQuery css():

    $('#myFlexbox').css('display', 'flex');
    
    0 讨论(0)
  • 2021-02-06 21:20
    $('#myFlexbox').show({
        start: function() {
            $(this).css('display', 'flex');
        }
    });
    
    0 讨论(0)
  • 2021-02-06 21:32

    The idea is to create a custom function showFlex() similar to jQuery show() and call it with the element which need to have the display:flex; property.

    jQuery Solution

    $.fn.showFlex = function() {
      this.css('display','flex');
    }
    

    $('#myFlexbox').showFlex();

    JavaScript Solution

    Object.prototype.showFlex = function() {
        this.style.display = 'flex';
    }
    

    document.getElementById('myFlexbox').showFlex();

    Hope this helps.

    0 讨论(0)
  • 2021-02-06 21:33

    just use

    .class{display: none}
    
    .class[style*='display: block']{
        display: flex !important;
    }
    

    when jquery add styl attribut css will be applied, works perfectly on Chrome and Mozilla

    0 讨论(0)
  • 2021-02-06 21:33

    You should wrap your display: flex content with an element.

    <div id="flexWrapper">
        <ul id="myFlexbox"></ul>
    </div>
    

    bind your javascript show/hide to '#flexWrapper'

    0 讨论(0)
  • 2021-02-06 21:33

    The class solution works, yes. Here's a different approach I came up with:

    Javascript Function

    function showFlex(element) {
    
        var flexContainer = document.getElementById(element);
    
        flexContainer.setAttribute("style", "display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: flex;");
    }
    

    To use it:

    showFlex( "yourIDhere" );
    

    Note that the ID doesn't need the # when you call the function.

    Example:

    https://codepen.io/florantara/pen/aLeyYb/

    0 讨论(0)
提交回复
热议问题