鍍金池/ 問答/HTML5  HTML/ 在html5中將<div>放在<canvas>之上?

在html5中將<div>放在<canvas>之上?

我在html中建立了一個<canvas>標(biāo)簽準(zhǔn)備繪制背景,后將網(wǎng)頁內(nèi)容放在一個<div>中,在之后發(fā)現(xiàn)<canvas>總是遮擋住<div>內(nèi)容,使用position:relative、z-index等屬性都沒達(dá)到預(yù)想的效果(極有可能是初學(xué)者代碼使用錯誤),請各位大神幫忙解決,以下是html和css的代碼。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <link href="css/first.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <canvas id="one" class="one1"></canvas>
    <script type="text/javascript">
    var canvas=document.getElementById('one');
    one.width="1440";
    one.height="2000";
    one.style.border="0px solid";
    var ctx=one.getContext("2d");
    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(500,0);
    ctx.lineTo(0,500);
    ctx.closePath();
    ctx.fillStyle = '#9aebff';
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(1092,0);
    ctx.lineTo(1440,0);
    ctx.lineTo(1440,2000);
    ctx.lineTo(768,2000);
    ctx.closePath();
    ctx.fillStyle = '#9aebff';
    ctx.fill();
    </script>

        <div class="main">
<!-- 以下是對問題沒有影響的網(wǎng)頁內(nèi)容 -->
        <div class="first">
            <a href="#" class="infirst1">首頁
            </a>
            <a href="#" class="infirst2">版塊介紹
            </a>
            <a href="#" class="infirst3">各個部門
            </a>
        </div>
        <div class="second">
            各個部門
            <p class="insecond2">Various&nbsp;departments
            </p>
        </div>
        <div class="third">
            <p class="inthird">采編部</p>
            <p>對待工作認(rèn)真,踏實(shí)肯干,有團(tuán)隊(duì)合作精神</p>
            <p>熱愛文學(xué)寫作、有良好的語言表達(dá)能力和溝通能力</p>
            <p>對攝影、攝像感興趣(不要求較高的專業(yè)基礎(chǔ))</p>
            <p>思路開闊、清晰,具有一定創(chuàng)新能力</p>
            <div class="one">
            </div>
        </div>
        <hr class="line"/>
        <div class="fourth">
            <p class="infourth">美編部</p>
            <p>具有良好的電腦作圖能力和色彩感,并有獨(dú)特的創(chuàng)意及審美理念</p>
            <p>具有良好的圖片處理能力和策劃能力</p>
            <p>對PS、AI等專業(yè)軟件具有一定興趣(不要求較高的專業(yè)基礎(chǔ))</p>
            <div class="two">
            </div>
        <div>
        <hr class="line2"/>
        <div class="fifth">
            <p class="infourth">技術(shù)部</p>
            <p>熱衷于程序、微信新功能的開發(fā),對于WEB開發(fā)有獨(dú)到見解</p>
            <p>要求有較強(qiáng)的學(xué)習(xí)能力、調(diào)試能力</p>
            <div class="three">
            </div>
        </div>
        <hr class="line3"/>
        <div class="sixth">
            <p class="infourth">維新工作室</p>
            <p>具有一定的原創(chuàng)創(chuàng)作能力,善于發(fā)現(xiàn)生活中的亮點(diǎn)</p>
            <p>看待問題視角獨(dú)特,思路創(chuàng)新</p>
            <div class="four">
            </div>
        </div>
        <hr class="line3"/>
        <div class="seventh">
            <p class="infourth">微博工作室</p>
            <p>會使用微博、人人等交際軟件</p>
            <p>具有一定的互動能力、文字編寫能力和創(chuàng)新意識</p>
        </div>
        <div class="five">
        </div>
        </div>
    </div>

</body>
</html>
.container{
    width:1440px;
    height:2000px;
    margin:0 auto;
}
.bg{
    width:1440px;
    height:2000px;
}
.main{
    width:1440px;
    height:2000px;
    position:relative;
    top:-2000px;
    z-index:20;
}

/*以下是對問題沒有影響的網(wǎng)頁內(nèi)容*/

.first{
    width:544px;
    height:60px;
    background:#51afd9;
    float:right;
    margin-top:34px;
    font-size:17px;
    color:#3783a9;
    line-height:60px;
}
.infirst1{
    font-size:17px;
    text-decoration:none;
    font-family:"黑體";
    margin-left:246px;
}
.infirst2{
    font-size:17px;
    text-decoration:none;
    font-family:"黑體";
    margin-left:30px;
}
.infirst3{
    font-size:17px;
    text-decoration:none;
    font-family:"黑體";
    margin-left:30px;
}
.infirst1:hover{
    color:#ffffff;
}
.infirst2:hover{
    color:#ffffff;
}
.infirst3:hover{
    color:#ffffff;
}
.second{
    text-align:center;
    font-size:28px;
    color:#255474;
    font-family:"黑體";
    float:left;
    margin-left:495px;
    margin-top:115px;
}
.third{
    width:941px;
    height:334px;
    color:#255474;
    float:left;
    margin-left:250px;
    font-family:"黑體";
}
.inthird{
    margin-top:80px;
    font-size:22px;
}
.one{
    width:243px;
    height:243px;
    background-image:url(../images/one.png);
    overflow:hidden;
    border-radius:50%;
    float:right;
    margin-top:-210px;
}
.line{
    width:906px;
    height:3px;
    background-color:#84bbe1;
    float:left;
    margin-left:250px;
}
.fourth{
    width:941px;
    height:334px;
    color:#255474;
    float:left;
    margin-left:250px;
    font-family:"黑體";
}
.infourth{
    margin-top:80px;
    font-size:22px;
}
.two{
    width:243px;
    height:243px;
    background-image:url(../images/two.png);
    overflow:hidden;
    border-radius:50%;
    float:right;
    margin-top:-190px;
}
.line2{
    width:906px;
    height:3px;
    background-color:#84bbe1;
    float:left;
    margin-top:58px;
}
.fifth{
    width:941px;
    height:334px;
    color:#255474;
    float:left;
    font-family:"黑體";
}
.three{
    width:243px;
    height:243px;
    background-image:url(../images/three.png);
    overflow:hidden;
    border-radius:50%;
    float:right;
    margin-top:-155px;
}
.line3{
    width:906px;
    height:3px;
    background-color:#84bbe1;
    float:left;
}
.sixth{
    width:941px;
    height:334px;
    color:#255474;
    float:left;
    font-family:"黑體";
}
.four{
    width:243px;
    height:243px;
    background-image:url(../images/four.png);
    overflow:hidden;
    border-radius:50%;
    float:right;
    margin-top:-155px;
}
.seventh{
    width:941px;
    height:334px;
    color:#255474;
    float:left;
    font-family:"黑體";
}
.five{
    width:243px;
    height:243px;
    background-image:url(../images/five.png);
    overflow:hidden;
    border-radius:50%;
    float:right;
    margin-top:-295px;
}

目標(biāo)網(wǎng)頁效果圖

回答
編輯回答
玄鳥
    .main {
      width: 1440px;
      height: 2000px;
    }
        canvas{
      position: absolute;
      z-index: -1;
    }
2018年1月9日 08:47