简体   繁体   English

将内部div根据其编号对齐

[英]Align inner divs to center based on their number

I have 4 div elements inside another div. 我在另一个div中有4个div元素。 From the application the user can choose how many divs to be displayed. 用户可以从应用程序中选择要显示的div数。

Whenever he chooses less than 4 I want the remaining divs to be align to the center. 每当他选择小于4时,我都希望其余的div与中心对齐。 I manage to do this with Jquery, can it be done using css?? 我设法用Jquery做到这一点,可以使用CSS完成吗? Below is my jquery code, the problem with it is that when the page loads the user cand see for few miliseconds the divs unaligned. 下面是我的jquery代码,它的问题是,当页面加载时,用户可以看到几毫秒的div未对齐。

I don't want that, I want the divs to be align soon as the pages is loaded(refreshed) 我不希望那样,我希望div在页面加载(刷新)后尽快对齐

HTML 的HTML

<div id="alignColumns">
     <div class="col-lg-3 wow zoomIn" style="padding: 0;"></div>    
     <div class="col-lg-3 wow zoomIn" style="padding: 0;"></div>               
     <div class="col-lg-3 wow zoomIn" style="padding: 0;"></div> 
     <div class="col-lg-3 wow zoomIn" style="padding: 0;"></div> 
</div>

jquery jQuery的

alignCols = function () {
    if (($("#alignColumns > div").length) == 3)
    {
        $("#alignColumns").css('margin-right','-133px')
    }
    else if (($("#alignColumns > div").length) == 2)
    {
        $("#alignColumns").css('margin-right', '-250px')
    }
    else if( ($("#alignColumns > div").length) == 1)
    {
        $("#alignColumns").css('margin-right', '-390px')
    }
};
window.onload = function () {
    alignCols();
};

Working Fiddle 工作小提琴

I think you want something like this : 我想你想要这样的东西:

WORKING : DEMO 工作:演示

HTML 的HTML

<center>
<div id="alignColumns">
             <div class="col-lg-3 wow zoomIn">DIV 1 <br><br><br><button id="1">Remove</button></div>    
             <div class="col-lg-3 wow zoomIn">DIV 2 <br><br><br><button id="2">Remove</button></div>               
             <div class="col-lg-3 wow zoomIn">DIV 3 <br><br><br><button id="3">Remove</button></div> 
             <div class="col-lg-3 wow zoomIn">DIV 4 <br><br><br><button id="4">Remove</button></div> 
</div>
</center>

JS JS

$('button').click(function()
                {
                    var a = this.id; 
                    if(a==1)
                    {
                        $(".wow:first-child").hide();
                    }

                    else if(a==2)
                    {
                        $(".wow:nth-child(2)").hide();
                    }

                    else if(a==3)
                    {
                        $(".wow:nth-child(3)").hide();
                    }

                    else if(a==4)
                    {
                        $(".wow:last-child").hide();
                    }

                    else
                    {
                        alert("EXCEPTION");
                    }

                });

CSS 的CSS

#alignColumns
{
    display:block;
    padding:10px;
    width:90%;
    position:relative;
    background:#ddd;
    height:auto;
    overflow:auto;
    vertical-align:center;
}

.wow
{
    display:inline-block;
    width:23.8%;
    position:relative;
    padding:5px 0px;
    margin:0px 1px;
    background:#333;
    text-align:center;
    height:90px;
    color:white;
}

I would accomplish what you described by adding flex-box to the mix, I've personally used it in tandem with bootstrap and it works brilliantly. 通过将flex-box添加到混音中,我可以完成您所描述的事情,我个人将其与bootstrap一起使用,并且效果很好。

Here's a jsFiddle with a demonstration of both forced ordering and fluid centering without jQuery. 这是一个jsFiddle,演示了没有jQuery时的强制排序和流体居中。

jsFiddle jsFiddle

The below code is a demonstration of how you can add flex-box to your project. 以下代码演示了如何将flex-box添加到项目中。 Make sure you place it after bootstrap or it may not work. 确保将其放在引导程序之后,否则可能无法工作。 Worst case scenario you can add an !important tag but doing so highly frowned upon by the community. 在最坏的情况下,您可以添加!important标记,但这样做却引起社区的高度反对。

.flex-container {
    display: -webkit-flex;
    display:    -moz-flex;
    display:     -ms-flex;
    display:      -o-flex;
    display:         flex;
    -webkit-justify-content:center;
    -moz-justify-content: center;
    justify-content: center;
}

.flex-item {
// add options
}

.one {
    -moz-order: 1;
    -webkit-order: 1;
    order: 1;
}
.two {
    -moz-order: 2;
    -webkit-order: 2;
    order: 2;
}
.three {
    -moz-order: 3;
    -webkit-order: 3;
    order: 3;
}
.four {
    -moz-order: 4;
    -webkit-order: 4;
    order: 4;
}   

An excellent, easy to understand resource on flex box: CSS-Tricks. 弹性框上的一个很好的,易于理解的资源: CSS技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM