EDIT: I originally wrote this as a response to this thread (http://
groups.google.com/group/rubyonrails-spinoffs/browse_thread/thread/
a735c534f89fc5cf/8d8546811cae5ee8), but it wouldn't let me respond
that that thread (presumably because of it's age). Rather than just
throw it away, I figured I'd post it here in case in can help anyone
else.
I built a little class to help me do just that. Here is my
implementation: (using Prototype 1.6)
var pointerTracker = Class.create({
initialize: function(){
this.x = null;
this.y = null;
this.bodyEl = $(document.body);
//Bind event listener.
this.boundUpdatePosition =
this.updatePosition.bindAsEventListener(this);
this.start();
},
updatePosition: function(event){
this.x = event.pointerX();
this.y = event.pointerY();
},
start: function(){
this.bodyEl.observe('mousemove', this.boundUpdatePosition);
},
stop: function(){
this.bodyEl.stopObserving('mousemove',
this.boundUpdatePosition);
},
within: function(element){
//is the cursor within the given element? Return bool
var el = $(element);
var vpo = el.viewportOffset();
return (this.x > vpo[0] &&
this.x < vpo[0] + el.getWidth() &&
this.y > vpo[1] &&
this.y < vpo[1] + el.getHeight());
}
});
Once you have that included in your page, you can use it like this...
//Start capturing mouse position.
var pointer = new pointerTracker();
//Close the toolbar if the mouse is not hovering over it. Check ever 3
seconds.
new PeriodicalExecuter(function(pe){
if (!pointer.within('tool-bar')){
pe.stop();
pointer.stop();
$('tool-bar').hide(); //Close the toolbar!
}
}, 3);
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---