为什么我在画布下看到div的背景

时间:2019-05-17 09:16:36

标签: javascript css

我正在尝试在div中滚动画布。问题是,当我尝试滚动时,看到的是下面div的红色背景中的红线部分,而不是我希望看到的所有内容都是黑色的。

   <div id="Div1" style=" float: left; width: 150px; height: 200px; background: red; overflow:scroll; ">
    <canvas id="myCanvas1" width="200px" height="500px" style="border:0px solid #ff0000;">asdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdf</canvas>
</div>

var c = document.getElementById("myCanvas1");
var ctx = c.getContext("2d");
ctx.fillRect(0, 0, 200, 500);
ctx.fillStyle = "#00ff00";  //<======= here
ctx.font = '35px serif';
ctx.fillText('Hello world', 0, 40);


canvas { border: 0px solid black; }

1 个答案:

答案 0 :(得分:0)

display: block;添加到canvas

var c = document.getElementById("myCanvas1");
var ctx = c.getContext("2d");
ctx.fillRect(0, 0, 200, 500);
ctx.fillStyle = "#00ff00"; //<======= here
ctx.font = '35px serif';
ctx.fillText('Hello world', 0, 40);
canvas {
  border: 0px solid black;
display: block;
}
<div id="Div1" style=" float: left; width: 150px; height: 200px; background: red; overflow:scroll; ">
<canvas id="myCanvas1" width="200px" height="500px" style="border:0px solid #ff0000;">asdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdf</canvas>
</div>