Skip to content Skip to sidebar Skip to footer

Track Only Mouse Move Of Hovered Child Element In A Nested Div Tag

I have an environment where there can be n number of nested div tag.I have to track the mouse move moment of the mouse only in child div. I have the following code. the result is

Solution 1:

You should use event.stopPropagation(), check snippet below.

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

Hope this helps.


var count = 0;
$('div').on({
  mousemove: function(event) {
    event.stopPropagation();
    $('.mousemovement').append('<li>MouseMove' + $(this).attr('class') + '</li>');

    if (count > 10) {
      $('.mousemovement').html('');
      count = 0;
    }
    count++;
  }
});
.outer {
  background-color: #aeaeae;
  height: 200px;
  width: 200px;
  float: left;
}

.inner {
  margin: 5px0px;
  background-color: #ccc;
  height: 100px;
  width: 100px;
  float: left;
}

.inner2 {
  margin: 5px0px;
  background-color: green;
  height: 60px;
  width: 60px;
  float: left;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="outer"><divclass="inner"><divclass="inner2"></div></div></div><ulclass="mousemovement"></ul>

Post a Comment for "Track Only Mouse Move Of Hovered Child Element In A Nested Div Tag"