javascript - 3个div并排溢出

标签 javascript html css

几个小时以来我一直在追寻这个问题,这是代码:

<!DOCTYPE html>
<html>
<head>
<style>
html, body{
    height: 100%;
    max-height: 100%;
    overflow:hidden;
}


.controls{
    display: table;
    height: 10%;
    margin-top: 1%;
    width: 100%;
}
#w1 {
    width:25%;
}
#can
    float: left;
    padding: 0;
    margin: 0;
    top: 0;
}
#canTwo{
    float: left;
    padding: 0;
    margin: 0;
    top: 0;

}
textarea { 
    outline: none;
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
    font-size: 1.25vw; 
    height: 100%;
    width: 100%;
}
#w2{
    width:50%;
}
#w3{ 
    width:25%;
}

.controlbuttons {
  display: table-cell;
  height: 100%;

}

</style>
</head>
<body>


<div class="controls">
    <div class="controlbuttons" id="w1"><canvas id = "can" width = "0" height = "0"></div>
    <div class="controlbuttons" id="w2"><textarea rows="3" cols="50"></textarea></div>
    <div class="controlbuttons" id="w3"><canvas id = "canTwo" width = "0" height = "0"></div>
</div>

</div>
<script>

document.addEventListener("DOMContentLoaded", function(event) {
   fitToContainer();
});
var canvas = document.getElementById("can"),
    ctx    = canvas.getContext('2d'),
    canvasTwo = document.getElementById("canTwo"),
    ctxTwo    = canvasTwo.getContext('2d');
function fitToContainer(){

    var control = document.getElementsByClassName("controlbuttons")[0];
    var h = control.clientHeight;
    var w = control.clientWidth;
    canvas.height = h;

    canvas.width = w;

    canvasTwo.height = h;

    canvasTwo.width = w;

    ctx.fillStyle = "green";
    ctx.fillRect(0, 0, 5000, 5000);

    ctxTwo.fillStyle = "green";
    ctxTwo.fillRect(0, 0, 5000, 5000);


}

</script>
</body>
</html>

jsfiddle: https://jsfiddle.net/ca3uw837/

基本上我有一个文本区域,它的宽度占页面的 50%,正好在中间,有两个 Canvas 到它的一侧,宽度为 25%。 我试图让它们完美对齐(相同的高度,一个紧挨着另一个)但这是它在我的电脑上的样子: enter image description here

我该怎么办?使用 flex 盒子?我不确定我是否知道如何实现它,因为 Canvas 的大小调整技术非常棘手。非常感谢您抽出宝贵时间。

最佳答案

flexbox 应用于 .controls 以对齐子元素。同时将 box-sizing: border-box 应用到 textbox 作为默认填充添加文本框的 100% 高度。 border-box 将使填充包含高度。

document.addEventListener("DOMContentLoaded", function(event) {
  fitToContainer();
});
var canvas = document.getElementById("can"),
  ctx = canvas.getContext('2d'),
  canvasTwo = document.getElementById("canTwo"),
  ctxTwo = canvasTwo.getContext('2d');

function fitToContainer() {

  var control = document.getElementsByClassName("controlbuttons")[0];
  var h = control.clientHeight;
  var w = control.clientWidth;
  canvas.height = h;

  canvas.width = w;

  canvasTwo.height = h;

  canvasTwo.width = w;

  ctx.fillStyle = "green";
  ctx.fillRect(0, 0, 5000, 5000);

  ctxTwo.fillStyle = "green";
  ctxTwo.fillRect(0, 0, 5000, 5000);


}
html,
body {
  height: 100%;
  max-height: 100%;
  overflow: hidden;
}

.controls {
  display: flex;
  height: 10%;
  margin-top: 1%;
  width: 100%;
}

#w1 {
  width: 25%;
}

#can float: left;
padding: 0;
margin: 0;
top: 0;

}
#canTwo {
  float: left;
  padding: 0;
  margin: 0;
  top: 0;
}
textarea {
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  font-size: 1.25vw;
  height: 100%;
  width: 100%;
  box-sizing: border-box;
}
#w2 {
  width: 50%;
}
#w3 {
  width: 25%;
}
.controlbuttons {
  height: 100%;
}
<div class="controls">
  <div class="controlbuttons" id="w1"><canvas id="can" width="0" height="0"></div>
    <div class="controlbuttons" id="w2"><textarea rows="3" cols="50"></textarea></div>
    <div class="controlbuttons" id="w3"><canvas id = "canTwo" width = "0" height = "0"></div>
</div>

关于javascript - 3个div并排溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43965815/

相关文章:

javascript - 如何使用 Babel 为 Internet Explorer 11 构建 React 应用程序?

javascript - (添加鼠标滚轮功能)使用 DOMmouseScroll 和鼠标滚轮调用函数

javascript - slider 菜单消失得太快

html - 在保持宽高比的同时使整个图像呈圆形

javascript - HTML/JS Canvas 在对象之间绘制线

javascript - TypeScript Angular 2 响应订阅

php - 如何在php中做href

css - html中的多表对齐

html - 如何使用递归字符串连接构建带有小计的 HTML 表?

html - 为什么我的 html 没有排队(相同的代码,不同的结果)