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

标签 javascript jquery html css canvas

我有一个横跨整个屏幕的 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)");
});

提前致谢。

最佳答案

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

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

关于javascript - 将事件悬停在更高 z-index 覆盖的元素上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489192/

相关文章:

javascript - 将对象数组的 AJAX 响应转换为 JSON 对象

jquery - 设置已检查 p :selectOneRadio item 的 td

javascript - 根据最后一行所有字段中的内容隐藏表列 - jQuery

html - ul 和子菜单 ul 的 wordpress 附加类

javascript - 双非运算符

javascript - 使用 Jquery 打印窗口后页面未重新加载

jquery - 如何在具有 defaultValue 的可选字段上使用 jQuery Validator 自定义方法?

javascript - 如何在javascript中为字符串编写上标

javascript - 有没有办法调整图像大小并让图像映射保持在同一个地方? (html/Javascript/CSS)

jquery - 改变输入值