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

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Event » Mouse Event 
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 -->
<!-- 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
w_w_w.__j__a___v___a__2__s._c__o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.