画布背景使锚无法点击

时间:2011-12-11 13:14:42

标签: javascript css html5 canvas

我发现有关使用HTML5 canvas元素创建动态背景是否可行的混合证据。这是一个似乎成功完成它的人:

http://radikalfx.com/files/anibg/

我已成功将我的canvas元素定位为背景,但它使我的链接无法点击。情况如下:

HTML:

<div id='container'>

... other header stuff ...
   <canvas id='background'>
   </canvas>

   <!-- Can't touch this *MC Hammer Shuffle* -->
   <a href='#'>test</a>
... footer stuff ...

</div>

CSS:

/* Everything's z-index is now 1 */
#container
{
position: relative;
min-height:100%;
width:100%;
z-index:1; 
}

/* Make the canvas z-index 0 */
#background
{
position: absolute;
top: 0;
width:100%;
height:100%;
z-index: 0;
}

JavaScript:

 // Onload Draw an ellipse
// I've got jCanvas installed (jQuery plugin) to use the drawArc() method
// This bit can be replaced with whatever test code you want. 
function load()
{
init_canvas();
$("canvas").drawArc({
        fillStyle: "black",
         x: 100, y: 100,
          radius: 50
    });
}

// Make the canvas the appropriate size
function init_canvas()
{
canvas = document.getElementById("background");
    canvas.width = document.width;
    canvas.height = document.height;
    canvasW = canvas.width;
    canvasH = canvas.height;
}

干杯!

2 个答案:

答案 0 :(得分:4)

您在CSS中使用#container为其他所有内容提供z-index为1,但您从未在页面上放置元素#container

将您的HTML更改为以下内容,它将按预期工作:

<canvas id='background'></canvas>
<div id="container">
    <a href='#'>test</a>
</div>

JSFiddle:http://jsfiddle.net/ht6c8/

答案 1 :(得分:0)

position: absolute的每个元素放在其他未定位的项目上,即使其他元素放在DOM之后。因此,您只需为锚元素设置position: relative