【发布时间】:2019-07-04 07:25:49
【问题描述】:
我对 jQuery 完全陌生。 我想要做的是过滤某些事件进入 DIV。
我的页面中有关注
<div id="overlay"></div>
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
叠加有以下样式
<style>
#overlay {
position: fixed; /* Sit on top of the page content */
display: block; /* Hidden by default */
width: 100%; /* Full width (cover the whole page) */
height: 100%; /* Full height (cover the whole page) */
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(1,0,0,0.5); /* Black background with opacity */
z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
pointer-events: auto
}
</style>
我有以下代码
$(document).on(
{
mousedown:mouseEvent,
mouseup:mouseEvent,
});
$("GameDiv").on(
{
mousedown:divEvent,
mouseup:divEvent,
});
function divEvent(e)
{
console.log("div event"+e);
}
function mouseEvent(e)
{
console.log("doc event" + e);
var evnt = jQuery.Event(e.type,e);
$("GameDiv").trigger(evnt)
}
现场示例:
$(document).on(
{
mousedown:mouseEvent,
mouseup:mouseEvent,
});
$("GameDiv").on(
{
mousedown:divEvent,
mouseup:divEvent,
});
function divEvent(e)
{
console.log("div event"+e);
}
function mouseEvent(e)
{
console.log("doc event" + e);
var evnt = jQuery.Event(e.type,e);
$("GameDiv").trigger(evnt)
}
#overlay {
position: fixed; /* Sit on top of the page content */
display: block; /* Hidden by default */
width: 100%; /* Full width (cover the whole page) */
height: 100%; /* Full height (cover the whole page) */
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(1,0,0,0.5); /* Black background with opacity */
z-index: 200; /* Specify a stack order in case you're using a different order for other elements */
pointer-events: auto
}
<div id="overlay"></div>
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
我正在获取文档鼠标事件。 但我没有收到第二个 div 事件。
我做错了什么?
有更好的方法吗?
【问题讨论】:
-
因为 position: fixed 和 z-index: 200 for overlay div,你永远无法收到 GameDiv 的事件
-
我想做的是使用这个覆盖过滤事件。并将选定的事件触发到 div 中。有没有办法做到这一点?
标签: javascript jquery events triggers mousedown