.mousemove()S2C Home « Events « .mousemove()

Mousemove event handler.

Description

The .mousemove() method is used to bind an event handler to the JavaScript mousemove event or trigger that event on the specified element.

  • The mousemove event is sent to an element when the mouse pointer is moved within the element it is bound to.
  • If it is a requirement to capture the the actual mouse coordinates you can examine the event object by passing it to the mousemove event handler function to determine the .pageX and .pageY properties of the event object. These properties provide the X and Y coordinates of the mouse pointer relative to the top-left corner of the document.

Syntax


Signature Description
.mousemove( )Trigger the mousemove JavaScript event on the specified element.
.mousemove( [eventData], handler(eventObject) )Bind an event handler to the mousemove JavaScript event, optionally passing an object of data.

Parameters


Parameter Description Type
eventDataAn object of data to pass to the event handler.Object
handler( eventObject )A function to execute each time the event is triggered.Function

Return

A jQuery object.

.mousemove( ) Examplego to top of page Top

Trigger the mousemove JavaScript event on the specified element.

  • This signature is a shortcut for .trigger('mousemove').

In the example below we show two new messages in the 'p' elements below every time the mouse moves over the image with the id of 'curry7'.

When the mouse moves while over the image, we trigger off the mousemove JavaScript event on the 'image'. This then fires off the $('#curry7').mousemove(function(){}) code which outputs the messages.


$(function(){
  $('#curry7').mousemove(function(event) {
    var pageCoords = '(' + event.pageX + ', ' + event.pageY + ' )';
    var viewableCoords = '(' + event.clientX + ', ' + event.clientY + ' )';
    $('#animspan1').text('( event.pageX, event.pageY ) : ' + pageCoords);
    $('#animspan2').text('( event.clientX, event.clientY ) : ' + viewableCoords);
  });
  $('#curry7').mousemove();
});

a picture of curry

The page coordinates are:

The viewable page coordinates are:

.mousemove( [eventData], handler(eventObject) ) Examplego to top of page Top


  • This signature is a shortcut for .bind('mousemove', handler).

Bind an event handler to the mousemove JavaScript event, optionally passing an object of data.

In the example below we show a new message in the 'p' element with an id of 'scrollspan7' each time the 'div' element with an id of 'div15' below is moved over.

When the mouse button moves over the division, the mousemove JavaScript event fires off the $('#div15').mousemove({ param1: '#scrollspan7', param2: 'mousemove', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText2(event). The map we specify, in our case { param1: '#scrollspan7', param2: 'mousemove', param3: '**JavaScript event triggered** ' } gets tagged onto the event.data property. We then access this parameter in the function via event.data.param and use it as part of the appended data.


$(function(){
  $('#div15').mousemove({ param1: '#scrollspan7', param2: 'mousemove ', 
                         param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append(event.data.param2 + ''<code>' + event.data.param3+ ''</code>');
  }
});

a picture of curry



We will show a message here.

go to home page Homepage go to top of page Top