简体   繁体   English

如果使用CSS3 translate / transform更改元素位置,则不会触发Mouseenter / Mouseover事件

[英]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. 我正在通过更新Y轴来翻译(通过jQuery / CSS3)一个#wrapper div。

I've attached mouseenter / mouseleave events to child elements of #wrapper . 我已经附加mouseenter / mouseleave事件的子元素#wrapper

When #wrapper translates, its child comes under mouse one by one ( even if mouse does not move ). #wrapper翻译时,它的孩子一个接一个地进入鼠标(即使鼠标不移动)。 And this does not fire the mouseenter , mouseleave events. 这并不会触发mouseentermouseleave事件。

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. 我希望将.block的背景改为red ,但它没有发生。

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. 返回正在调用elementFromPoint方法的文档中的元素,该元素位于给定点下面的最顶层元素。 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. 要获取元素,请通过坐标(以CSS像素为单位)指定相对于包含文档的窗口或框架中最左上角点的点。

Browser support from Here : 这里的浏览器支持

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

Solution 1 Working Example *: 解决方案1 工作示例 *:

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 : 解决方案2

Use debounce from lodash or underscore libraries, to reduce load on client. 使用debouncelodash下划线库,以减少对客户端负载。

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM