Tôi đã thử nó cho bạn với mã sau. Div được đặt trên đỉnh của phần tử canvas giống như Matthew mô tả nó. Vì vậy, nên làm việc cho bạn
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Canvas demo</title>
<style type="text/css">
#canvasSection{ position:fixed;}
</style>
<script type="text/javascript">
function draw()
{
//paint the text
var canvas = document.getElementById('canvasSection');
var context = canvas.getContext('2d');
context.fillStyle = '#00f';
context.font = 'italic 30px sans-serif';
context.textBaseline = 'top';
context.font = 'bold 30px sans-serif';
context.strokeText('Your Text!!', 0, 0);
//paint the square
var canvasSquare = document.getElementById('canvasSquare');
var ctxSquare = canvas.getContext('2d');
ctxSquare.fillStyle='#FF0000';
ctxSquare.fillRect(0, 100,50,100);
}
</script>
</head>
<body onLoad="draw()">
<canvas id="canvasSection">Error, canvas is not supported</canvas>
<div>TestText</div>
</body>
</html>
thử style = "position: fixed; top: 0; left: 0" và nó hoạt động tốt. thanx! – user306708
vừa phát hiện ra rằng thiết lập z-index: -1; là cần thiết để đặt canvas phía sau các phần tử khác. – user306708
@ fx42: Không đặt 'z-index: -1', nó gây rối với một số trình duyệt. Thay vào đó, bọc phần tử còn lại của bạn vào một div và đặt 'z-index: 1' trên div đó. –