Created
June 16, 2011 19:49
-
-
Save jethrolarson/1030087 to your computer and use it in GitHub Desktop.
isMouseWithin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($){ | |
| window.util ={mousepos:{}}; | |
| //Constantly tracks mouse position | |
| $(document).bind('mousemove.util', function(e){ | |
| //adapted from http://www.quirksmode.org/js/events_properties.html | |
| if (!e) var e = window.event; | |
| if (e.pageX || e.pageY) { | |
| util.mousepos.left = e.pageX; | |
| util.mousepos.top = e.pageY; | |
| } | |
| else if (e.clientX || e.clientY) { | |
| util.mousepos.left = e.clientX + document.body.scrollLeft | |
| + document.documentElement.scrollLeft; | |
| util.mousepos.top = e.clientY + document.body.scrollTop | |
| + document.documentElement.scrollTop; | |
| } | |
| }); | |
| util.isMouseWithin = function(selector){ | |
| var $el = $(selector), | |
| pos = $el.position(), | |
| w = $el.outerWidth(), | |
| h = $el.outerHeight(); | |
| return util.mousepos.left > pos.left | |
| && util.mousepos.left < pos.left + w | |
| && util.mousepos.top > pos.top | |
| && util.mousepos.top < pos.top + h; | |
| }; | |
| })(jQuery);//jQuery 1.0+ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created this for a hover augment which ended up being impractical. Saving for future use.