I'am a big mootools fan. But now I have to port a mootools-snipe to jquery and I do not get it.
I' am using this code:
var prev_m_x = 0;
var prev_m_y = 0;
window.addEvent('mousemove',function(e){
prev_m_x=e.page.x;
prev_m_y=e.page.y;
});
Element.Events.enterfromleft ={
base: 'mouseover',
condition: function(event){
var elpos = this.getPosition();
if(prev_m_x<=elpos.x) return true; return;
}
};
Element.Events.enterfromright ={
base: 'mouseover',
condition: function(event){
var elpos = this.getPosition();
if(prev_m_x>=(elpos.x+this.getWidth())) return true; return;
}
};
to define my events.
In the end I'am using this events in the following way:
el.addEvents({
'enterfromleft':function(e){
...
},
'enterfromright':function(e){
} ...
I tried to do something like this in jquery but without success. Please see here: http://jsfiddle.net/tFw89/33/
What I have to do in jquery to define a private event? Thanks for any help in advance.
You can write a plugin
(function($) {
var prev_m_x = 0;
var prev_m_y = 0;
$(document).on('mousemove', function(e) {
prev_m_x = e.pageX;
prev_m_y = e.pageY;
});
$.fn.mouseMove = function(type) {
return this.each(function() {
var $this = $(this);
$this.on('mouseover', function(e) {
if (prev_m_x <= e.pageX) {
$this.trigger('enterfromleft')
} else {
$this.trigger('enterfromright')
}
});
});
};
})(jQuery);
$('#myButton').on('enterfromleft', function(){
console.log('enterfromleft')
});
$('#myButton').on('enterfromright', function(){
console.log('enterfromright')
});
$('#myButton').mouseMove()
Demo: Fiddle
A fix related to tracking mouse move, register mouse move event on document, rather than to the element.