在更高的z-index覆盖的元素上悬停事件?

时间:2014-03-18 19:30:00

标签: javascript jquery html css canvas

我有一个横跨整个屏幕的画布元素(canvas-mouse) - 它的目的是在特定大小(grabsize)的鼠标周围绘制50%的不透明度圆圈。页面上还会有一些div中的图像。我希望这些图像可以点击/悬停,但我也希望canvas-mouse中的50%不透明度圆圈显示在它们之上。

有没有办法实现这个目标?

HTML:

<canvas id="canvas-mouse" class="fullscreen"></canvas>
<div class="object die"><img src="images/Die_d6.svg"></div>

CSS:

html, body {
    width:  100%;
    height: 100%;
    margin: 2px;
    overflow: hidden;
    color: #FFFFFF;
    background-color: #2C744C;
}

canvas.fullscreen {
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: -1;
}

.object {
    position: absolute;
}

#canvas-mouse {
    z-index: 10;
}

JavaScript的:

CanvasRenderingContext2D.prototype.drawCircle = function(xpos, ypos, radius, linewidth, linecolor, fill) {
    if(typeof(linewidth)==="undefined") {
        linewidth = 1;
    }
    if(typeof(linecolor)==="undefined") {
        linecolor = "#000000";
    }

    this.beginPath();
    this.arc(xpos, ypos, radius, 0, 2*Math.PI, false);
    this.lineWidth = linewidth;
    if(typeof(fill)!=="undefined") {
        this.fillStyle = fill
        this.fill();
    }
    this.strokeStyle = linecolor;
    this.stroke();
}
CanvasRenderingContext2D.prototype.maximize = function() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
}
mousectx = $("#canvas-mouse")[0].getContext("2d");
mousectx.maximize();

//Dice handlers
$(".object.die").hover(function() {
    //Hover event goes here
})
$(".object.die").mousedown(function() {
    //Click event goes here
})

//Mouse movement handler
$(document).mousemove(function(e){
    //Get the mouse positions and put them in {mouse}
    mouse.x = e.pageX;z
    mouse.y = e.pageY;

    //Redraw the grab circle
    mousectx.clearCanvas();
    mousectx.drawCircle(mouse.x,mouse.y,grabsize,1,"#000000","rgba(0,0,255,0.5)");
});

提前致谢。

1 个答案:

答案 0 :(得分:6)

尝试使用pointer-events: none。此规则告诉浏览器忽略元素。鼠标事件不会被它接收,但会通过&#39;

#canvas-mouse {
    z-index: 10;
    pointer-events: none;
}
相关问题