Called from an onmousedown event handler : Mouse Event « Event « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Event » Mouse Event 
Called from an onmousedown event handler
  
/*
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
*/

/**
 * PortableDrag.js:
 * beginDrag() is designed to be called from an onmousedown event handler.
 * elementToDrag may be the element that received the mousedown event, or it
 * may be some containing element.  event must be the Event object for the
 * mousedown event.  This implementation works with both the DOM Level 2
 * event model and the IE Event model.
 **/
function beginDrag(elementToDrag, event) {
    // Compute the distance between the upper-left corner of the element
    // and the mouse click. The moveHandler function below needs these values.
    var deltaX = event.clientX - parseInt(elementToDrag.style.left);
    var deltaY = event.clientY - parseInt(elementToDrag.style.top);

    // Register the event handlers that will respond to the mousemove events
    // and the mouseup event that follow this mousedown event.  
    if (document.addEventListener) {  // DOM Level 2 Event Model
  // Register capturing event handlers
        document.addEventListener("mousemove", moveHandler, true);
  document.addEventListener("mouseup", upHandler, true);
    }
    else if (document.attachEvent) {  // IE 5+ Event Model
  // In the IE Event model, we can't capture events, so these handlers
  // are triggered when only if the event bubbles up to them.
  // This assumes that there aren't any intervening elements that
  // handle the events and stop them from bubbling.
  document.attachEvent("onmousemove", moveHandler);
  document.attachEvent("onmouseup", upHandler);
    }
    else {                            // IE 4 Event Model
  // In IE 4 we can't use attachEvent(), so assign the event handlers
  // directly after storing any previously assigned handlers so they 
        // can be restored.  Note that this also relies on event bubbling.
  var oldmovehandler = document.onmousemove;
  var olduphandler = document.onmouseup;
  document.onmousemove = moveHandler;
  document.onmouseup = upHandler;
    }

    // We've handled this event.  Don't let anybody else see it.  
    if (event.stopPropagationevent.stopPropagation();   // DOM Level 2
    else event.cancelBubble = true;                       // IE

    // Now prevent any default action.
    if (event.preventDefaultevent.preventDefault();     // DOM Level 2
    else event.returnValue = false;                       // IE

    /**
     * This is the handler that captures mousemove events when an element
     * is being dragged.  It is responsible for moving the element.
     **/
    function moveHandler(e) {
  if (!ee = window.event;  // IE event model

        // Move the element to the current mouse position, adjusted as
  // necessary by the offset of the initial mouse click.
  elementToDrag.style.left = (e.clientX - deltaX"px";
  elementToDrag.style.top = (e.clientY - deltaY"px";

  // And don't let anyone else see this event.
  if (e.stopPropagatione.stopPropagation();       // DOM Level 2
  else e.cancelBubble = true;                       // IE
    }

    /**
     * This is the handler that captures the final mouseup event that
     * occurs at the end of a drag.
     **/
    function upHandler(e) {
  if (!ee = window.event;  // IE event model

  // Unregister the capturing event handlers.
  if (document.removeEventListener) {    // DOM Event Model
      document.removeEventListener("mouseup", upHandler, true);
      document.removeEventListener("mousemove", moveHandler, true);
  }
  else if (document.detachEvent) {       // IE 5+ Event Model
      document.detachEvent("onmouseup", upHandler);
      document.detachEvent("onmousemove", moveHandler);
  }
  else {                                 // IE 4 Event Model
      document.onmouseup = olduphandler;
      document.onmousemove = oldmovehandler;
  }

  // And don't let the event propagate any further.
  if (e.stopPropagatione.stopPropagation();       // DOM Level 2
  else e.cancelBubble = true;                       // IE
    }
}


           
         
    
  
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. Mousedown event handler of an object within a Layer
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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.