Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouseenter/Mouseover events do not fire if CSS3 translate/transform is used to change element position

I am translating ( via jQuery / CSS3 ) a #wrapper div, by updating Y axis.

I've attached mouseenter / mouseleave events to child elements of #wrapper.

When #wrapper translates, its child comes under mouse one by one ( even if mouse does not move ). And this does not fire the mouseenter , mouseleave events.

However, events are fired when element has scrollbar and scrolled by mousewheel ( instead of translating ).

Is this a default behavior ? If yes, any workaround ?

Demo

Try scrolling with mousewheel, without moving mouse. I expect to change the background of .block to red color, but it's not happening.

like image 884
Jashwant Avatar asked Feb 06 '14 17:02

Jashwant


People also ask

What is the difference between Mouseenter () and mouseover () event?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

Which event is triggered when a mouse is hovering over an element?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.

What is the event opposite to mouseover?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves.

What events can be used to change of font when the mouse cursor is moved over and away from an element?

The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children. Tip: This event is often used together with the onmouseover event, which occurs when the pointer is moved onto an element, or onto one of its children.


1 Answers

Example:

document.elementFromPoint(x, y);

Definition from Here:

Returns the element from the document whose elementFromPoint method is being called which is the topmost element which lies under the given point. To get an element, specify the point via coordinates, in CSS pixels, relative to the upper-left-most point in the window or frame containing the document.

Browser support from Here:

  • Internet Explorer 5.5+
  • Mozilla FireFox 3+
  • Safari Win 5+
  • Google Chrome 4+
  • Opera 10.53+

Solution 1 Working Example*:

var currentMousePos = { x: -1, y: -1 };
$(document).mousemove(function(event) {
    currentMousePos.x = event.pageX;
    currentMousePos.y = event.pageY;
});
$(containerElement).mousewheel(function(event) {
    $(elementClass).trigger('mouseleave');
    var element = document.elementFromPoint(currentMousePos.x, currentMousePos.y);
    $(element).trigger('mouseenter');
});

* there are some bugs, but you get the idea

Solution 2:

Use debounce from lodash or underscore libraries, to reduce load on client.

var currentMousePos = { x: -1, y: -1 };
$(document).mousemove(function (event) {
    currentMousePos.x = event.pageX;
    currentMousePos.y = event.pageY;
});
var debounced = _.debounce(function () {
    $(elementClass).trigger('mouseleave');
    var element = document.elementFromPoint(currentMousePos.x, currentMousePos.y);
    $(element).trigger('mouseenter');
}, 150);
$(containerElement).mousewheel(function (event) {
    debounced();
});
like image 171
skmasq Avatar answered Sep 19 '22 02:09

skmasq