Ad
JQuery Trigger Document Event Into A DIV
I am totally new to jQuery. What I wanted to do is filter certain events from going into a DIV.
I have following in my page
<div id="overlay"></div>
<div id="GameDiv" style="width:1280px; height: 720px;"></div>
overlay has following style
<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>
I have following code
$(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)
}
Live Example:
$(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>
I am getting the document mouse event. But I am not receiving 2nd div event.
What did I do wrong?
Is there a better way of doing this?
Ad
Answer
To get the both the events you can follow the below code snippet.
$(document).on(
{
mousedown: mouseEvent,
mouseup: mouseEvent,
});
$("#GameDiv").on("click", 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 {
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>
If you really need to do it with mousedown
and mouseup
events u can follow the below snippet.
#overlay {
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>
<script>
$(document).on(
{
mousedown: mouseEvent,
mouseup: mouseEvent,
});
$("#GameDiv").on("mousedown", divEvent);
$("#GameDiv").on("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)
}
</script>
Another solution
Refer the snippet below to call both the events at the same time
#overlay {
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>
<script>
$(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)
}
</script>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad