【问题标题】:Fixed Header at top of scrolled list. Flex Layout with all columns same height修复了滚动列表顶部的标题。所有列高度相同的 Flex 布局
【发布时间】:2016-01-24 15:51:02
【问题描述】:

似乎无法让它工作。想要为不随 div 滚动的蓝色和绿色 div 添加一个固定的标题。让这件事变得复杂的是:

  • 左侧的红色画布是唯一宽度缩小的元素
  • 画布元素保持宽高比,同时高度缩小
  • 随着 Canvas 高度缩小,蓝色和绿色 div 的高度也会缩小。

JSFiddle

HTML

<div class="container">
  <div class="left">
    <canvas id="draw" width="250" height="300"></canvas>
  </div>
  <div class="center"><div id="center" class="scroll"></div></div>
  <div class="right"><div id="right" class="scroll"></div></div>
</div>

CSS

.container {
  max-width: 650px;
  margin: 0 auto;
  border: 1px solid black;
  display: flex;
}
.container > div {
  position: relative;
  min-width: 0;
}
.left {
  max-width: 250px;
  width: auto;
}
.left canvas {
  display: block;
  max-width: 100%;
  height: auto;
}
.center {
  flex: 0 0 200px;
  overflow-y: scroll;
  background-color: blue;
}
.right {
  flex: 0 0 200px;
  overflow-y: scroll;
  background-color: green;
}
.scroll {
  position: absolute;
  width: 100%;
}

JavaScript

var c = document.getElementById("draw");
ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 250, 300);
ctx.fillStyle = "white";
ctx.font = "bold 40pt Arial";
ctx.fillText("CANVAS", 10, 100);

tmp = "";
for (var i = 0; i < 100; i++) {
    tmp += "<p>" + i + "</p>";
}

document.getElementById("center").innerHTML = tmp;
document.getElementById("right").innerHTML = tmp;

【问题讨论】:

    标签: html css


    【解决方案1】:

    想给不随div滚动的蓝色和绿色div添加一个固定的header

    检查以下是否适合您:

    var c = document.getElementById("draw");
    ctx = c.getContext("2d");
    ctx.fillStyle = "red";
    ctx.fillRect(0, 0, 250, 300);
    ctx.fillStyle = "white";
    ctx.font = "bold 40pt Arial";
    ctx.fillText("CANVAS", 10, 100);
    
    tmp = "";
    for (var i = 0; i < 100; i++) {
      tmp += "<p>" + i + "</p>";
    }
    
    document.getElementById("center").innerHTML = tmp;
    document.getElementById("right").innerHTML = tmp;
    .container {
      max-width: 650px;
      margin: 0 auto;
      border: 1px solid black;
      display: flex;
    }
    .container > div {
      position: relative;
      min-width: 0;
    }
    .left {
      max-width: 250px;
      width: auto;
      flex: 1;
    }
    .left canvas {
      display: block;
      max-width: 100%;
      height: auto;
    }
    .center {
      flex: 0 0 200px;
      background-color: #0000ff;
    }
    .right {
      flex: 0 0 200px;
      background-color: green;
    }
    .scroll {
      position: absolute;
      width: 100%;
      top: 40px;
      bottom: 0;
      overflow-y: auto;
    }
    .header {
      height: 40px;
      line-height: 40px;
    }
    .center .header {
      background-color: #aaaaff;
    }
    .right .header {
      background-color: #aaff00;
    }
    <div class="container">
      <div class="left">
        <canvas id="draw" width="250" height="300"></canvas>
      </div>
      <div class="center">
        <div class="header">fixed header</div>
        <div id="center" class="scroll"></div>
      </div>
      <div class="right">
        <div class="header">fixed header</div>
        <div id="right" class="scroll"></div>
      </div>
    </div>

    【讨论】:

    • 在 FF 和 Chrome 中很好,但在 IE 中不行,但那是我的错。我原来的小提琴和你的更新在 IE 中不起作用,但我的真实网站可以。一定是我这边出了点问题,我已经想通了,只是从来没有放过小提琴。没什么好担心的。
    • 别担心。我编辑了我之前的评论,以表明我原来的小提琴在 IE 中也不起作用。虽然也许值得为其他人解决这个问题。将您的修复标记为解决方案。谢谢。虽然不能24小时奖励赏金。不认为这是一个非常困难的问题,但无法弄清楚,早期的否定导致其他人不看我的问题。
    • @Mike 问题在于.left 元素中缺少flex: 1。现在应该在 IE 11+ 上工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多