`mousemove`并触发自定义事件 [英] `mousemove` and triggering a custom event

查看:142
本文介绍了`mousemove`并触发自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mousemove 事件中:

$(document).on('mousemove', function( e ){
    console.log( e.pageX );
    console.log( e.pageY );
});

如你所见,我们可以使用 pageX pageY 获取 x y 鼠标位置的纵坐标。但是,我想要的是在 mousemove 上触发我的自定义事件,并希望得到这些 pageX pageY 我自定义事件中的值。更清楚的是,我想做的是:

as you can see, we can use pageX and pageY to get the x and y co-ordinates of mouse position. But, what I want is to trigger a custom event of mine on mousemove and would like to get these pageX and pageY values in that custom event of mine. To be more clear, what I would like to do is:

$(document).on('mousemove', function(){
    $(document).trigger('myevent');
});

$(document).on('myevent', function( e ){
    // console.log( e.pageX );
    // console.log( e.pageY );
});

有没有办法访问这些 pageX pageY myevent

Is there any way to access these pageX and pageY in myevent?

推荐答案

.trigger() 允许通过其参数传递其他数据。您可以致电

.trigger() allows to pass additional data via its arguments. You can call

$(document).on('mousemove', function( event ){
    $(document).trigger('myevent', event);
});

现在您可以访问整个原始事件自定义事件代码中的对象。

Now you have access to the whole original event object within your custom event code.

这篇关于`mousemove`并触发自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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