代码之家  ›  专栏  ›  技术社区  ›  Vadim H

在中间换行(css/js/jq)

  •  2
  • Vadim H  · 技术社区  · 8 年前

    一行内联块如何被收缩的父块包装,从而产生相等(或几乎相等)的行?

    所以不要像这样包装:

    像这样包装:

    如果块数不均匀,如:

    =

    enter image description here

    这样包装:

    enter image description here

    如果块数不均匀,如:

    3 回复  |  直到 8 年前
        1
  •  0
  •   SkillGG    8 年前

    grid-template-columns docment.getElementById('bottomblocks').style.gridTemplateColumns

    https://jsfiddle.net/Lhbqdt2z/

    Coding Tech Talk

    W3Schools

    here

        2
  •  0
  •   Louys Patrice Bessette    8 年前

    $(window).on("load resize",function(){
      $(".container div").css({"clear":"initial"});
      var wrapped = false;
      var wrappedAt = 0;
      var wrappedNtimes =0;
      var pos = $(".container div").first().offset();
      var n = $(".container div").length;
    
      $(".container div").each(function(index){
        if(!wrapped){
    
          if( ($(this).offset().top != pos.top)){
            console.log("Wrapped at "+index+" out of "+n);
            wrapped = true;
            wrappedAt = index;
            wrappedNtimes++;
          }
          pos=$(this).offset();
        }
      });
    
      if(wrapped){
        // Force enhanced wrapping... .oO(lol)
        console.log("--"+wrappedAt+"--");
        var half = Math.ceil(n/(wrappedNtimes+1));
    
        $(".container div").each(function(){
          if( $(this).index() != 0 && ($(this).index())%half == 0){
            $(this).css({"clear":"left"});  // zero-based.
          }
        });
      }
    });
    

    CodePen demo

        3
  •  0
  •   Vadim H    8 年前

    <br>

    // Make sure that the last row of blocks doesn't have 2 less blocks than all
    // the previous rows. Assume that all blocks are equal size.
    var blocks = sharing.find('.btn');
    
    //what's the parent width
    var parentWidth = blocks.parent().width();
    
    //how many blocks can fit in such a width
    var maxNumOfBlocksInOneRow = Math.floor(parentWidth / blocks.outerWidth(true));
    
    //repeatable code
    var calcNumOfBlocksInLastRow = function(){
      var lastRowFull = blocks.length % maxNumOfBlocksInOneRow ? false : true;
      if (lastRowFull) {
        return maxNumOfBlocksInOneRow;
      } else {
        return blocks.length % maxNumOfBlocksInOneRow;
      }
    }
    
    //do we have more blocks than row's maximum?
    if (blocks.length > maxNumOfBlocksInOneRow) {
      //how many blocks would the last row have
      var numOfBlocksInLastRow = calcNumOfBlocksInLastRow();
    
      //if the last row is missing more than 1 block, try with 1 less block in each row
      while (numOfBlocksInLastRow < maxNumOfBlocksInOneRow - 1) {
        maxNumOfBlocksInOneRow--;
        numOfBlocksInLastRow = calcNumOfBlocksInLastRow();
      }
    
      //insert <br> at the end of each row
      jQuery('<br>').insertAfter(blocks.filter(':nth-child(' + maxNumOfBlocksInOneRow + 'n)'));
    }