首页 > 解决方案 > Html5 有一个动画画布作为 Div 元素的背景?

问题描述

我想让这个 JS - https://codepen.io/jkiss/pen/OVEeqK - 作为我首页上 div 元素的背景。

html

<script src="{% static 'home/animated.js' %}"></script>
        <div class = "content-section">
            <div id = "canvas-wrap">
                <canvas id="nokey"></canvas>
                <div class = "bg-1 media" id = "overlay">
                    <div class = "container">
                        <img src = "../../../media/pfp.jpg"  class = "profile-img img-thumbnail rounded-circle">
                        <div class = "media-body intro">
                            <h1 class = "">My Name</h1>
                            <h4>Aspiring Web Developer</h4>
                        </div>
                    </div>
                </div>
            </div>

CSS

body{
    background-color: #222222;
    font:inherit;
    font-family: Titillium;
    margin:0;
    padding:0;
}

#canvas-wrap { position:absolute; width:100px; height:50px; z-index:0 }
#canvas-wrap canvas { position: absolute; top:0; left:0; z-index:0 }

#nokey{
    background-color: darkblue;
}

.content-section{
    margin-left: 50px;
    margin-right: 50px;
    margin-bottom: 50px;
    background: aliceblue;
}

.bg-1 .container{
    display: flex;
    align-items: center;
    justify-content: center;
}

.intro{
    flex: none;
    margin-top: 200px;
    margin-bottom: 200px;
    padding-left: 25px;
    color: white;
}

我能做些什么?目前,画布占据了整个屏幕,并在其后面创建了 div。

标签: javascripthtmlcsscanvashtml5-canvas

解决方案


使用z-indexCSS 属性将 div 移动到更高的其他元素:

#nokey{
    background-color: darkblue;
    z-index: 1; // or more, if other elements has one
}


推荐阅读