control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/mousemove.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
5.1 KiB
JSON

{"name":"mousemove","type":"method","title":".mousemove()","deprecated":null,"removed":null,"desc":"Bind an event handler to the \"mousemove\" JavaScript event, or trigger that event on an element.","categories":["events/mouse-events","version/1.0","version/1.4.3"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":{"desc":"A function to execute each time the event is triggered.","name":"handler(eventObject)","type":"Function"}},{"added":"1.4.3","argument":[{"desc":"An object containing data that will be passed to the event handler.","name":"eventData","type":"PlainObject","optional":"true"},{"desc":"A function to execute each time the event is triggered.","name":"handler(eventObject)","type":"Function"}]},{"added":"1.0"}],"examples":{"desc":"Show the mouse coordinates when the mouse is moved over the yellow div. Coordinates are relative to the window, which in this case is the iframe.","code":"\n$( \"div\" ).mousemove(function( event ) {\n var pageCoords = \"( \" + event.pageX + \", \" + event.pageY + \" )\";\n var clientCoords = \"( \" + event.clientX + \", \" + event.clientY + \" )\";\n $( \"span:first\" ).text( \"( event.pageX, event.pageY ) : \" + pageCoords );\n $( \"span:last\" ).text( \"( event.clientX, event.clientY ) : \" + clientCoords );\n});\n","height":"300","css":"\n div {\n width: 220px;\n height: 170px;\n margin: 10px 50px 10px 10px;\n background: yellow;\n border: 2px groove;\n float: right;\n }\n p {\n margin: 0;\n margin-left: 10px;\n color: red;\n width: 220px;\n height: 120px;\n padding-top: 70px;\n float: left;\n font-size: 14px;\n }\n span {\n display: block;\n }\n","html":"\n<p>\n <span>Move the mouse over the div.</span>\n <span>&nbsp;</span>\n</p>\n<div></div>\n"},"longdesc":"\n <p>This method is a shortcut for <code>.on( \"mousemove\", handler )</code> in the first two variations, and <code>.trigger( \"mousemove\" )</code> in the third.</p>\n <p>The <code>mousemove</code> event is sent to an element when the mouse pointer moves inside the element. Any HTML element can receive this event.</p>\n <p>For example, consider the HTML:</p>\n <pre><code>\n&lt;div id=\"target\"&gt;\n Move here\n&lt;/div&gt;\n&lt;div id=\"other\"&gt;\n Trigger the handler\n&lt;/div&gt;\n&lt;div id=\"log\"&gt;&lt;/div&gt;\n </code></pre>\n <p>The event handler can be bound to the target:</p>\n <pre><code>\n$( \"#target\" ).mousemove(function( event ) {\n var msg = \"Handler for .mousemove() called at \";\n msg += event.pageX + \", \" + event.pageY;\n $( \"#log\" ).append( \"&lt;div&gt;\" + msg + \"&lt;/div&gt;\" );\n});\n </code></pre>\n <p>Now when the mouse pointer moves within the target button, the messages are appended to &lt;div id=\"log\"&gt;:</p>\n <p>\n <samp>Handler for .mousemove() called at (399, 48)</samp>\n <br/>\n <samp>Handler for .mousemove() called at (398, 46)</samp>\n <br/>\n <samp>Handler for .mousemove() called at (397, 44)</samp>\n <br/>\n <samp>Handler for .mousemove() called at (396, 42)</samp>\n <br/>\n </p>\n <p>To trigger the event manually, apply <code>.mousemove()</code> without an argument:</p>\n <pre><code>\n$( \"#other\" ).click(function() {\n $( \"#target\" ).mousemove();\n});\n </code></pre>\n <p>After this code executes, clicks on the Trigger button will also append the message:</p>\n <p>\n <samp>Handler for .mousemove() called at (undefined, undefined)</samp>\n </p>\n <p>When tracking mouse movement, you usually need to know the actual position of the mouse pointer. The event object that is passed to the handler contains some information about the mouse coordinates. Properties such as <code>.clientX</code>, <code>.offsetX</code>, and <code>.pageX</code> are available, but support for them differs between browsers. Fortunately, jQuery normalizes the <code>.pageX</code> and <code>.pageY</code> properties so that they can be used in all browsers. These properties provide the X and Y coordinates of the mouse pointer relative to the top-left corner of the document, as illustrated in the example output above.</p>\n <p>Keep in mind that the <code>mousemove</code> event is triggered whenever the mouse pointer moves, even for a pixel. This means that hundreds of events can be generated over a very small amount of time. If the handler has to do any significant processing, or if multiple handlers for the event exist, this can be a serious performance drain on the browser. It is important, therefore, to optimize <code>mousemove </code>handlers as much as possible, and to unbind them as soon as they are no longer needed.</p>\n <p>A common pattern is to bind the <code>mousemove</code> handler from within a <code>mousedown</code> hander, and to unbind it from a corresponding <code>mouseup</code> handler. If implementing this sequence of events, remember that the <code>mouseup</code> event might be sent to a different HTML element than the <code>mousemove</code> event was. To account for this, the <code>mouseup</code> handler should typically be bound to an element high up in the DOM tree, such as <code>&lt;body&gt;</code>.</p>\n "}]}