[英]Unable to use <canvas> element inside <div> element
我無法在 <div> 元素中添加 <canvas> animation 。 使用當前代碼,animation 占據整頁,我想將 <canvas> 封裝在 <div> 中,以便 animation 進入單個部分。 類似於 <div><canvas> </canvas></div>
HTML:
<canvas></canvas>
<h2>hi</h2>
CSS:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
width: 100%;
height: 100vh;
font-family: 'Poppins', sans-serif;
background-color: #121212;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 0;
}
h2 {
font-size: 80px;
color: white;
position: relative;
}
將position:relative
div 包裹在 canvas 周圍。 使 canvas 不fixed
,而是absolute
以 div 框架作為參考。
* { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; font-family: 'Poppins', sans-serif; background-color: #121212; display: flex; align-items: center; justify-content: center; } canvas { width: 100%; height: 100%; position: absolute; background: rgba(255, 0, 0, 0.3); top: 0; left: 0; z-index: 0; } h2 { font-size: 80px; color: white; position: relative; }.my-canvas { position: relative; width: 500px; height: 200px; border: 1px solid red; }
<div class="my-canvas"> <canvas></canvas> <h2>hi</h2> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.