我正在尝试在 div 内滚动 Canvas 。问题是,当我尝试滚动时,我看到下面 div 的红色背景的红线部分,而不是我希望看到的一切都是黑色的。
HTML:
<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>
JavaScript:
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);
CSS:
canvas { border: 0px solid black; }
最佳答案
添加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>
关于javascript - 为什么我在 Canvas 下看到 div 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183280/