Mousedown event handler of an object within a Layer : Mouse Event « Event « JavaScript DHTML






Mousedown event handler of an object within a Layer

  

/*
Examples From
JavaScript: The Definitive Guide, Fourth Edition

Legal matters: these files were created by David Flanagan, and are
Copyright (c) 2001 by David Flanagan.  You may use, study, modify, and
distribute them for any purpose.  Please note that these examples are
provided "as-is" and come with no warranty of any kind.

David Flanagan
*/

<script>
/**
 * This function is intended for use in a mousedown event handler of an object
 * within a Layer.  The first argument must be a Layer object.  The second
 * argument must be the Event object for the mousedown event.
 **/
function beginDrag(layerToDrag, event) {
    // This nested function responds to mousemove events and moves the layer.
    function moveHandler(event) {
  // Move the element to the current mouse position, adjusted as
  // necessary by the offset of the initial mouse click.
  layerToDrag.moveTo(event.pageX - deltaX, event.pageY-deltaY);

  // Don't take any default action, and don't propagate further.
  return false;
    }

    // This nested function handles mouseup events.
    // It stops capturing events, and de-register the handlers.
    function upHandler(event) {
  // Stop capturing and handling drag events
  document.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  document.onmousemove = null;
  document.onmouseup = null;

  // Don't take any default action, and don't propagate further.
  return false;
    }

    // Compute the distance between the upper left of the layer and and the
    // mouse click. The moveHandler function below needs these values.
    var deltaX = event.pageX - layerToDrag.left;
    var deltaY = event.pageY - layerToDrag.top;

    // Arrange to capture mousemove and mouseup events.
    // Then arrange to handle them using the functions defined below.
    document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
    document.onmousemove = moveHandler;
    document.onmouseup = upHandler;
}
</script>
<!-- Here's how we might use beginDrag() in Netscape 4 -->
<!-- Define a layer using CSS attributes -->
<div id="div1" style="position:absolute; left:100px; top:100px;">
<!-- Give the layer some content, and a mousedown event handler -->
<img src="images/plus.gif" width="20" height="20"
     onmousedown="if (event.modifiers & Event.SHIFT_MASK)
                      beginDrag(window.document.div1, event);">
</div>


           
         
    
  








Related examples in the same category

1.Catches and manages the mouse's events
2.Mouse and key event (IE)
3.Mouse in image and out
4.Image Mouse on and out
5.Mouse cross hairs
6.Mouse Drag and Drop
7. Creating a Rollover Effect
8.Codependent Link Tag and the onMouseOver Event
9.Which mouse button was clicked?
10.Which element was clicked
11.Mouse over event
12.Get component From Point (Mouse)
13.Cutting and Pasting under Script Control
14.Using Drag-Related Event Handlers
15. Using onDragEnter and onDragLeave Event Handlers
16.Using onMouseDown and onMouseUp Event Handlers
17.Dragging Elements with onMouseMove
18.Using the toElement and fromElement Properties
19. The onBeforeCopy Event Handler
20.Called from an onmousedown event handler
21.Cursor Arrival and Departure
22.Use Mouse over action to transfer url location
23.Get mouse position with on mouse move event (IE)
24.Get mouse position in mouse down event (IE)
25.H1 double click events
26.Return boolean value for on click event
27.Register mouse down event(IE)