.mousemove()
methodThe .mousemove()
method in jQuery is used to bind an event handler to the mousemove
event or trigger the mousemove
event on an element.
When the mouse pointer moves inside the element(including child elements), the event mousemove
is fired.
.mousemove()
method Syntax.mousemove() .mousemove(handler) .mousemove(eventData,handler)
Parameter | Type | Description |
---|---|---|
handler |
Function | A function to execute when the mousemove is triggered on the matched element. |
eventData |
Anything | Additional data passed to the event handler. |
.mousemove(handler)
and .mousemove(eventData,handler)
are shortcut for .on( "mousemove", handler )
.mousemove()
is shortcut for .trigger( "mousemove" )
.mousemove()
method ExampleTrigger the mousemove event on target:
HTML
<div id="target">target</div> <div id="btn"> Trigger mousemove event on target </div>
jQuery
$( "#target" ).mousemove(function() { console.log( "mousemove." ); }); $("#btn").click(function(){ $( "#target" ).mousemove(); }
.mousemove([eventData,]handler)
method ExampleFollowing is a simple example which change the element's background color when the mouse pointer enters or leaves the element.
function toHex(d) { return ("0"+(Number(d).toString(16))).slice(-2).toUpperCase() } function randColor(){ return ('#'+toHex(Math.round(Math.random() * 255))+toHex(Math.round(Math.random() * 255))+toHex(Math.round(Math.random() * 255))); } $("p").mousemove(function(){ $(this).css("background-color",randColor()); });
Show the mouse coordinates which are relative to the window, which in this case is the iframe.