{"id":324,"date":"2013-10-07T22:15:56","date_gmt":"2013-10-07T14:15:56","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=324"},"modified":"2018-09-04T13:07:00","modified_gmt":"2018-09-04T05:07:00","slug":"how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\/","title":{"rendered":"How to use jQuery to get mouse cursor coordinates when mouse moves"},"content":{"rendered":"<p>I was exploring on how to create drag and drop elements on a webpage. The first thing that I have to know is to detect my mouse cursor coordinates when my mouse moves. <\/p>\n<p>This post documents my exploration in using jQuery to detect mouse cursor coordinates when I move my mouse.<\/p>\n<h2>Registering a JavaScript callback function to listen to mouse movements<\/h2>\n<p>Getting notified when the mouse moves is easy with jQuery:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).mousemove(function(event) {\r\n    $('#mousePosition').html('Your mouse is currently at: ' \r\n                                + event.pageX +', '+ event.pageY);\r\n});\r\n<\/pre>\n<p>I first wrap the document object with the jQuery function. I then call the <code>mousemove<\/code> function, passing in a callback function that accepts an <code>event<\/code> object. By doing so I instructed jQuery that I want it to help me listen for mouse movements that happens on the entire webpage.<\/p>\n<p>In the callback function, I use jQuery to look for a element marked with <code>mousePosition<\/code> as its id and set the coordinates of the mouse as its html contents. The <code>pageX<\/code> and <code>pageY<\/code> attributes of the <code>event<\/code> object give me the coordinates of my mouse cursor. <\/p>\n<h2>The entire HTML document<\/h2>\n<p>After constructing the JavaScript codes, I proceed to construct the HTML document by referencing <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">my little HTML code reference<\/a>. I then include the JavaScript codes at the head section of the document. The following is the entire HTML document that I use for realizing this proof of concept.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n        $(document).ready(function(){\t   \r\n            $(document).mousemove(function(event){\r\n\t        $('#mousePosition').html('Your mouse is currently at: ' \r\n                                            + event.pageX +', '+ event.pageY);\r\n            }); \r\n\t});\r\n    &lt;script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=&quot;mousePosition&quot;&gt;\r\nNo mouse movement detected yet.\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div class=\"callToAction\">\n   <a target=\"_blank\" href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/using-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\" class=\"readMoreLink\">See this proof of concept<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5e\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5e&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5e&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F324&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I was exploring on how to create drag and drop elements on a webpage. The first thing that I have to know is to detect my mouse cursor coordinates when my mouse moves. <\/p>\n<p>This post documents my exploration in using jQuery to detect mouse cursor coordinates when I move my mouse.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,128,89,171],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5e","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/324"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}