`mousemove`并触发自定义事件 [英] `mousemove` and triggering a custom event
问题描述
在 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屋!