【发布时间】:2016-01-24 15:51:02
【问题描述】:
似乎无法让它工作。想要为不随 div 滚动的蓝色和绿色 div 添加一个固定的标题。让这件事变得复杂的是:
- 左侧的红色画布是唯一宽度缩小的元素
- 画布元素保持宽高比,同时高度缩小
- 随着 Canvas 高度缩小,蓝色和绿色 div 的高度也会缩小。
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;
【问题讨论】: