jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 从上到下对齐div,然后从左到右对齐大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个宽度为200px,高度为500px的主分区.根据一些事件,我需要将宽度为50px和高度为50px的子分区添加到主分区中.我需要从左上到下添加它并向右流动.

|---------------|
|       |       |   
|   1   |   4   |
|-------|-------|
|       |       |
|   2   |   5   |
|-------|-------|
|       |       |
|   3   |   6   |
|-------|-------|

我试过这些CSS,但它不起作用.

.SubDiv {
   height: 50px;
   width: 50px;
   BACkground: red;
}

.MainDiv {
   max-height: 200px;
   height: 200px;
   width: 200px;
   BACkground: blue;
   min-width: 100px;
   direction: inherit;
}

<div>
    <div id="SimulationPage">
       <div id = "ParentDiv" class="MainDiv">
       </div>
    </div>

    <input type='button' id = "adddiv" value='add div' />
</div>

$(document).ready(function () {
    $('#adddiv').bind('click',function (eventargs) {
        $("#ParentDiv").append('<div class="SubDiv"> </div>');
    });
});

有人可以帮我实现我想要的.

解决方法

你想要实现的目标不能仅仅在css中完成,因为你在父类中拥有未知数量的子项并且在左右浮动它们,你必须找到最中间的子项并从那里浮动它.

如果jQuery让你高兴(因为你已经在其中标记了这个问题)

here is a solution for dynamic count with jQuery

使用jQuery:

var count = $(".MainDiv").children().length; /*get @R_51_10586@l childs */
    var mid_child = Math.ceil(count / 2) + 1; /* find mid child */
    var x = 1; /*parameter to set negtaive margin,to push right float on toP*/
    var current_div_number = mid_child; /* div number to set margin */

    /*assign class name to 2nd half childs,to float them right 
  through css,can be done through jQuery too!!*/
    $(".MainDiv .SubDiv:nth-child(n+" + mid_child + ")").addClass("mid");


    /* check each .mid class and assign negative margin-toP*/
    $(".MainDiv .mid").each(function (indeX) {
        $(".MainDiv .SubDiv:nth-child(" + current_div_number + ")").css('margin-top',-(mid_child * 50) + (50 * X) + 'px');
        x = x + 1;
        current_div_number = current_div_number + 1;
    });

CSS

.MainDiv > div.mid {
    float:right;
    BACkground: green;
}

大佬总结

以上是大佬教程为你收集整理的jquery – 从上到下对齐div,然后从左到右对齐全部内容,希望文章能够帮你解决jquery – 从上到下对齐div,然后从左到右对齐所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。