Prevent mousemove events on children elements that are absolutely positioned

Exploring the intricacies of the mousemove event when it comes to absolute elements.

To showcase my findings, I have created a demo on Codepen.

The challenge is to capture the mousemove event on the #main element without affecting any of its absolutely positioned children.

HTML:

<div id="main">
  <div class="btn">Click Me</div>
</div>
<div id="output">

</div>

JS:

$(document).ready(function(){

  $('#main').on('mousemove',function( e ) {
  var message = "Mouse move ";
  message += e.pageX + ", " + e.pageY;
  $( "#output" ).html(message);
});

});

Answer №1

To prevent event handling for specific targets, you can simply identify the target in your event handler and exit the function early if it is one of those that should be blocked.

In this setup, it involves checking whether the target matches the #main element or jQuery's $event.currentTarget.

$('#main').on('mousemove', function(e) {
  // only interested in #main events
  // disregard events on other targets
  if (e.target !== e.currentTarget) return;
  var msg = "mouse move ";
  msg += e.pageX + ", " + e.pageY;
  $("#output").html(msg);
});
$('#main').on('mouseout', function() {
  $("#output").html('');
});
$('#main>.btn').on('click', e=> $("#output").html('can still click'));
#main {
  float: left;
  background: yellow;
  width: 400px;
  height: 400px;
  position: relative;
}

#main .btn {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 2;
  border: 0;
  background: blue;
  color: #FFF;
}

#main .btn .innerbtn {
  padding: 10px;
}

#output {
  display: inline-block;
  background: #efefef;
  width: 200px;
  position: absolute;
  right: 0;
  pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main">
  <div class="btn">
    <div class="innerbtn">Click Me</div>
  </div>
</div>
<div id="output">

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Link with javascript onClick attribute

Is there a way to combine a JavaScript "onClick" event with an HTML "a href" link? I would like for the first step to be triggered upon clicking the link using "onClick", and if that is successful, the user should then be redirected to another page using t ...

Could a complex, intricate clipping path be created using CSS 3?

Here is an example: Can this be achieved using only CSS? I wish to create two divs: A circle without any background or border. A div with a background. I aim to have point 1 clip the background from point 2. This will allow me to rotate the backgroun ...

The div is inexplicably shifting downward once the first three rows are shown correctly

After displaying the first 3 rows properly, the div mysteriously moves down. Could I be making a silly mistake? All images have the same width and height. Please see the attached image for reference. <?php // start first row echo "<hr class='m ...

Loading views and controllers on-the-fly in AngularJS

A new configuration tool is under development using Angular.JS. The user interface consists of two main sections: a left panel with a tree view listing all the configuration items and a right panel displaying screens for editing these items. There are appr ...

Discovering digits within a given text using a specific pattern and modifying them using JavaScript

I have a series of text inputs containing numbers with a specific pattern throughout the text. I would like to identify all numbers with this pattern, recalculate them, and replace them in the text. .... val="2xf" ......... vc="2.6xf" ...... value= ...

Place a div element on top of the others by using the onClick() event to "dock" it

Can someone help me with setting/docking a div element above another div? The div contains some other divs and form objects, serving as an inputbox dialog that should appear right above an element when clicked. I have set the onClick() event for a Click &l ...

The JQuery TextNTags plugin eliminates tag formatting once the trigger syntax has been modified

I have incorporated the JQuery TextNTags plugin into my web application. Here is the original code snippet: $.browser = { webkit: true }; $(function () { $('textarea.tagged_text').textntags({ triggers: {'!': { ...

Is there a way to trigger a function when the close button (cross icon) is clicked for an introjs-react step?

Exploring the functionalities of introjs-react, I have familiarized myself with the 2 props available - onExit and onComplete. While the former is triggered when a step is skipped (e.g., clicking outside the step on the overlay), the latter activates when ...

Modify a particular entry that is being looped through in PHP

Currently, I have coded the following: <div class="row"> <div class="col-lg-12"> <table id="usertable" class="table table-bordered table-hover text-center"> <thead> <th class="col-lg-1 text-center">User ID</th> ...

What are the steps to manipulate third party API data with the Node.js 'Request' module, prior to transmitting it to the frontend?

Being a newcomer to NodeJs, I took to Google for assistance but came up empty. My challenge lies in editing the json data retrieved from an API link and sending it to frontend AngularJS as is. app.get('/getCustomers', bodyParser.urlencoded({exte ...

Ensuring Vue.js correctly re-renders an array item

In my vue.js 2 project, I am working with an array that has the following structure: data() { return { ports: [ { id: 1, name: "example", "age": 10, scores: [ {index: 1, value: 100}, {index: 2, value: 200} ]}, { id: 2, ...

When the previous textbox is filled, the cursor will automatically move to the button

Utilizing an RFID reader where students tap their ID to display basic info, a hidden button on the form opens a modal with various purposes for selection. The challenge is shifting focus of cursor to the button and automatically clicking it when the last ...

"Troubleshooting: jQuery Find function not functioning correctly with HTML template

I am having trouble with a Shopify liquid code that I am trying to load into an HTML template <script type="text/template" id="description"> <div class="product-ddd"> {{ product.description }} </div> ...

Twitter typeahead not functioning properly when used in conjunction with ajax requests

I have limited experience in the frontend world and I'm currently working on getting this to function properly. $('#the-basics .typeahead').typeahead({ hint: true, highlight: true, minLength: 6 }, { source: function (query, ...

Firebase version 9 - Retrieve the content of a referenced document

I'm having trouble locating the documentation I need. Within my Firebase Firestore project, there is a collection called users. Within users, there are three collections: companies, policies, and stores. In the policies collection, I have fields for ...

Is there a glitch in Safari's CSS involving max-height?

Currently, I am in the process of developing a popup for a registration form. The CSS rules have been configured accordingly and after testing on various browsers, it has come to my attention that Safari is displaying an additional white space between elem ...

Switching back regular text from a superscript in document.execCommand

Currently, I'm developing a customized text editor that utilizes a contenteditable div with some basic formatting features. To enable superscript functionality, I'm employing the command document.execCommand("superscript", false, undefi ...

Having trouble with sharing feature in React Native for Android and iOS platforms?

I'm having trouble sharing pdf file links or image links in a react native mobile app web browser. I added a share button, but when the user clicks on it, they see options to share via WhatsApp, Gmail, messages, etc. However, when clicking on WhatsApp ...

The button component is unresponsive

An app was developed with a component containing signin and signup buttons on the right side. However, there is an issue where the Sign up button is not clickable. Below is the code snippet for the component => import React from "react"; expo ...

Tips for choosing an image using jQuery from an external HTML page

I'm attempting to embed an HTML page within a div and then individually select all the img tags within that page to display an overlay div on top of the images. Currently, I can insert the HTML into a div named "asd" and it seems like the jQuery is f ...