简体   繁体   English

在更多列中向左浮动CSS技巧

[英]Float Left in more columns CSS trick

I just tried to find something to resolve my problem in CSS but may be in jQuery. 我只是试图找到解决CSS问题的方法,但可能是在jQuery中。 On first look is very simple but strange I cant resolve this. 乍一看很简单,但奇怪的是我无法解决。 Could someone to give me a hint? 有人可以给我提示吗?

I have an array in JS: 我在JS中有一个数组:

var blocks = ["block1", "block2", "block3", "block4", "block5", "block6", "block7", "block8", "block9", "block10", "block11", "block12", "block13", "block14", "block15"];

I will parse it in this way: 我将以这种方式解析它:

for (var i = 0; i < blocks.length; i++) {
    var one_block = $("<div class='one_block'>" + blocks[i] + "</div>");
        $("all_blocks").append(one_block);
} 

<style>
 .one_block{
  width:20%;
  float:left;
 }
</style>

Now I have this result on my screen 现在我在屏幕上看到了这个结果

Such you understand this are all divs 这样你就知道这都是div

And this is HTML: 这是HTML:

block1   block2   block3   block4   block5

block6   block7   block8   block9   block10

block11   block12   block13   block14   block15

But I need to have this 但是我需要这个

block1   block4   block7   block10   block13

block2   block5   block8   block11   block14

block3   block6   block9   block12   block15

Thanks! 谢谢!

You can put the "blocks" into a columns, and use the modulus % to drop them into the right column in order. 您可以将“块”放入一列中,然后使用模数%将它们按顺序放入正确的列中。

Something like this would work: 这样的事情会起作用:

CSS CSS

 #column-1,#column-2,#column-3,#column-4,#column-5 {
  width:18%;
  float:left;
  border: 1px solid #333;
 }

HTML HTML

<div id="wrapper">
  <div id="column-1"></div>
  <div id="column-2"></div>
  <div id="column-3"></div>
  <div id="column-4"></div>
  <div id="column-5"></div>
</div>

JavaScript JavaScript的

var numberOfBlocks;
//get a number from the user to test how many blocks
while(isNaN(numberOfBlocks)) {
    numberOfBlocks = prompt('How many blocks do you want?')
}
//build the test array of blocks
var blocks = [];
for(var i = 1; i <= numberOfBlocks; i++) {
    blocks.push('block'+i)
 }


//determine the number of rows to use
rows = Math.ceil(blocks.length / 5);

//if the number of rows did not divide evenly, use modulus to find the number of columns that will need to be longer
numberOfLongColumns = (blocks.length % 5);

//keep track of the current column
column = 0;

//use an index, instead of i in the loop.  this allows us to reset the index if we encounter a column that has fewer elements
index = 0;

//loop over the array
for (var i = 0; i < blocks.length; i++) {

//if we've reached the end of a column...
    if(index % rows == 0) {
    //if it is the last of the longer columns...
    if(numberOfLongColumns > 0 && column == numberOfLongColumns) {
        //reset the index
      index = 0;
      //decrement the rows, so the next column is 1 element shorter
      rows--;
    }
    //move the pointer to the next column
    column++;
  }
  //increment the index
  index++;

  //add the element
  var one_block = $("<div>" + blocks[i] + "</div>");
  $("#column-"+column).append(one_block);
} 

Example Output 示例输出

block1 block5 block9 block13 block16 区块1区块5区块9区块13区块16
block2 block6 block10 block14 block17 区块2区块6区块10区块14区块17
block3 block7 block11 block15 block18 区块3区块7区块11区块15区块18
block4 block8 block12 block4 block8 block12

You can see it working here: https://jsfiddle.net/igor_9000/386rwry5/2/ 您可以在这里看到它的工作: https : //jsfiddle.net/igor_9000/386rwry5/2/

Just rearrange the array to suit your needs: 只需重新排列阵列即可满足您的需求:

var blocks = ["block1", "block4", "block3", "block2", "block5", "block6", "block7", "block8", "block9", "block10", "block11", "block12", "block13", "block14", "block15"];

Or however you want the order. 或者,您要订购。

CSS3 columns might do the trick. CSS3 columns可以解决问题。 It works similar to how newspaper columns work. 它的工作方式类似于报纸专栏的工作方式。 Best part is, it will re-size according to view-port (similar to flex-box .) See more 最好的部分是,它会根据图端口重新大小(类似于flex-box 。) 查看更多

.all_blocks {
  /* create 5 columns with 10% of view-port width */
  -webkit-columns: 10vw 5; 
  -moz-columns: 10vw 5;
  columns: 10vw 5;
}

See Demo 观看演示

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

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