如何在jquery上并排区分mouseout/leave事件? [英] How to differ mouseout/leave events by side on jquery?

查看:127
本文介绍了如何在jquery上并排区分mouseout/leave事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法知道鼠标事件是否已从元素的特定侧面离开?我的意思是,我有一个带有mouseover/mouseenter事件的DIV,并且仅当鼠标离开DIV的左侧和右侧,但是如果它是从底部或顶部离开的,则不应触发任何操作.

is there a way to know if the mouse event has left from a specific side of the element?I mean, I have a DIV with a mouseover/mouseenter event and I want to trigger the action only if the mouse leaves to the left side of the DIV and to the right, but if it leaves from bottom or top, it shouldn't trigger any action.

谢谢.

推荐答案

使用jQuery,您可以这样使用事件的offsetX属性:

With jQuery you could use the offsetX property of the event as so:

$('#element').mouseout(function(e) {
    if (e.offsetX < 0 || e.offsetX > $(this).width()) {
        alert('out the side!');
    }
});

我不认为该属性是可靠的跨浏览器(不带jQuery),并且我相信jQuery可以对其进行规范化(我已经在Chrome和IE7-9中对其进行了测试).基本上,该属性包含触发事件时光标相对于目标元素的位置.如果该值小于0或大于元素的宽度,则您知道光标离开元素边界时位于元素的左侧或右侧.

I don't think that property is reliable cross-browser (w/o jQuery) and I believe jQuery normalizes it (I've tested it in Chrome & IE7-9). Basically, the property contains where the cursor is relative to the target element at the time the event is fired. If the value is less than 0 or greater than the width of the element, you know the cursor was outside the left or right side of the element at the time it left the bounds of the element.

这篇关于如何在jquery上并排区分mouseout/leave事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆