【问题标题】:CSS - Scale flexbox content to fitCSS - 缩放 flexbox 内容以适应
【发布时间】:2020-10-21 21:59:01
【问题描述】:

假设我有一个用一些正方形填充的 flexbox...

const init = () => {
  const container = document.querySelector('.container')
  for(const i of Array(70).keys()){
    const square = document.createElement('div')
    square.classList.add('square')
    container.appendChild(square)
  }
}

init()
* {
  box-sizing: border-box;
}

.container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.square{
    background-color: orange;
    height: 8em;
    width: 8em;    
    margin: .2em;
    perspective: 1000px;
}
<div class="container">
</div>

我现在希望这些方块缩小,直到全部适合非滚动容器(当然,同时保持纵横比)。

我该怎么做?

【问题讨论】:

  • CSS 网格在这里会更好,相关:stackoverflow.com/a/54125439/8620333
  • 我同意 Temani。弹性盒子的大小是沿一维调整的。如果您需要沿两个维度调整大小,网格框是更好的选择。

标签: javascript html css flexbox


【解决方案1】:

这是一个非常人性化的解决方案。如果你把mainContainer的宽高设置得当,它会跟得上。

const init = () => {
  const container = document.querySelector('.container');
  const l = 70; //how many element you want
  const h = container.clientHeight;
  const w = container.clientWidth;
  for(const i of Array(l).keys()){
    const square = document.createElement('div')
    square.classList.add('square');
    const x = parseInt(Math.sqrt((w*h) / (l)));
    const xcount = Math.ceil(w/x);
    const xLength = Math.ceil(w / xcount);
    const ycount = Math.ceil((l/xcount));
    const yLength = parseInt(h / ycount);
    const edge = Math.min(xLength, yLength);
    square.style.width = edge + "px";
    square.style.height = edge + "px";
    container.appendChild(square)
  }
}

init()
* {
  box-sizing: border-box;
}

.mainContainer{
  width: 500px;
  height: 150px;
  min-height: 100%;
  background-color: #dfdfdf;
}

.container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.square{
    background-color: orange;
    border: solid 1px;
    height: 1px;
    width: 1px;    
}
<div class="mainContainer"><div class="container">
</div></div>

【讨论】:

    【解决方案2】:

    我试过了,但是当你调整大小时需要重新加载以使 clientWidth 检测到 并预测盒子的高度和宽度

    (function(){
        const init = () => {
            const container = document.querySelector('.container')
            for(const i of Array(70).keys()){
                const square = document.createElement('div')
                square.classList.add('square')
                var w = document.body.clientWidth;
                if (w > 800){
                    square.style.width = w / 385 + 'em';
                    square.style.height = w/ 395 + 'em';
                    square.style.margin = .3 + 'em';
                } else {
                    square.style.width = w / 475 + 'em';
                    square.style.height = w/ 485 + 'em';
                    square.style.margin = .2 + 'em';
                }
                container.appendChild(square)
            }
        }
    init()
    }())
    *{
      box-sizing: border-box;
      padding: 0;
      margin: 0;
    }
    
    .container {
        position: relative;
        max-height: 100vh;
        width: 100%;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
    }
    
    .square{
        background-color: orange;
        perspective: 1000px;
    }
    &lt;div class="container"&gt;&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2017-04-08
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 2011-01-20
      • 2010-12-03
      • 2011-11-19
      相关资源
      最近更新 更多