{"id":9847,"date":"2011-06-19T11:37:36","date_gmt":"2011-06-19T18:37:36","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=9847"},"modified":"2013-07-19T05:54:18","modified_gmt":"2013-07-19T12:54:18","slug":"calculate-distance-between-mouse-and-element","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/jquery\/calculate-distance-between-mouse-and-element\/","title":{"rendered":"Calculate Distance Between Mouse and Element"},"content":{"rendered":"
(function() {\r\n    \r\n    var mX, mY, distance,\r\n        $distance = $('#distance span'),\r\n        $element  = $('#element');\r\n\r\n    function calculateDistance(elem, mouseX, mouseY) {\r\n        return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.offset().left+(elem.width()\/2)), 2) + Math.pow(mouseY - (elem.offset().top+(elem.height()\/2)), 2)));\r\n    }\r\n\r\n    $(document).mousemove(function(e) {  \r\n        mX = e.pageX;\r\n        mY = e.pageY;\r\n        distance = calculateDistance($element, mX, mY);\r\n        $distance.text(distance);         \r\n    });\r\n\r\n})();<\/code><\/pre>\n

This code will calculate the distance between the mouse cursor and the center of an element. This can be useful for triggering a function when the mouse is within a certain distance of an element. Or, you can base the value of a property, such as the width, height, or opacity of the element, on the proximity of the mouse cursor.<\/p>\n","protected":false},"excerpt":{"rendered":"

(function() { var mX, mY, distance, $distance = $(‘#distance span’), $element = $(‘#element’); function calculateDistance(elem, mouseX, mouseY) { return Math.floor(Math.sqrt(Math.pow(mouseX – (elem.offset().left+(elem.width()\/2)), 2) + Math.pow(mouseY – (elem.offset().top+(elem.height()\/2)), 2))); } $(document).mousemove(function(e) { mX = e.pageX; mY = e.pageY; distance = calculateDistance($element, mX, mY); $distance.text(distance); }); })(); This code will calculate the distance between the mouse cursor […]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3245,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-snippet.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":14188,"url":"https:\/\/css-tricks.com\/almanac\/selectors\/f\/focus\/","url_meta":{"origin":9847,"position":0},"title":":focus","date":"September 6, 2011","format":false,"excerpt":"The :focus pseudo class in CSS is used for styling an element that is currently targeted by the keyboard, or activated by the mouse. Here is an example: textarea:focus { background: pink; } CodePen Embed Fallback Any element (most commonly\u00a0s and\u00a0