jQuery .show() a flex box

牧云@^-^@ 提交于 2020-05-10 06:53:33

问题


I have an element with a flexbox

<ul id="myFlexbox">

div#myFlexbox{
    display:flex;
}

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

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

now the element has a display of block instead of flex.

<ul id="myFlexbox" style="display: block;">

How can I use .hide and .show with flexboxes?


回答1:


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

$('#myFlexbox').css('display', 'flex');



回答2:


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:

<html>
  <head>
    <!-- CSS -->
  </head>
  <body>
    <!-- BODY PART -->
    <!-- SCRIPT TO HIDE AND SHOW -->
  </body>
</html>

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

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




回答3:


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




回答4:


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'




回答5:


Since other answers did not account for the possibility that you might be using the duration argument...

Short Answer: Use .hide for hiding the value and that shouldn't happen.

A little explanation:

The matched elements will be hidden immediately, with no animation. This is roughly equivalent to calling .css( "display", "none" ), except that the value of the display property is saved in jQuery's data cache so that display can later be restored to its initial value. If an element has a display value of flex and is hidden then shown, it will once again be displayed flex.

From documentation, Changed 'inline' to 'flex' to fit the contenxt!

So jQuery knows what you're hiding! If you've hidden it using .hide that is, And it knows how to show it again with the right display value.




回答6:


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/




回答7:


I know it is a little too late (3 years) but this is the way I managed to do it.

I created a rule in css for my div, for example #myDiv, that goes like this

#myDiv[style*='block'] {
    display: inline-flex !important;
}

It takes advantage of what jQuery does to the DOM, it applies 'display:block'. I used inline-flex because i needed it for what i'm doing, but it can be whatever you want. In this way the rule goes faster applied without any flickering.




回答8:


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.



来源:https://stackoverflow.com/questions/38491653/jquery-show-a-flex-box

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