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

10

我有一个canvas元素(canvas-mouse),它覆盖整个屏幕 - 它的目的是在鼠标周围绘制一个50%不透明度的圆形,大小为(grabsize)。页面上还有一些包含图片的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)");
});

预先致谢。


这个 https://dev59.com/s3A65IYBdhLWcg3wsg2B 可能会解决这个问题。 - T J
Canvas-mouse不应该有一个相对位置才能使z-index功能正常吗? - Rstew
1个回答

18

尝试使用 pointer-events: none。这个规则告诉浏览器忽略一个元素。它不会接收到鼠标事件,但是会让事件“穿透”。

#canvas-mouse {
    z-index: 10;
    pointer-events: none;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接