1 function dragDropEnable(element)
2 {
3 //Attach three "private" event handlers contained inside this function
4 element.AddEventListener("MouseLeftButtonDown",onMouseLeftButtonDown);
5 element.AddEventListener("MouseMove",onMouseMove);
6 element.AddEventListener("MouseLeftButtonUp",onMouseLeftButtonUp);
7
8 var dragging=false;
9 var lastPoint=null;
10
11 function onMouseLeftButtonDown(sender,mouseEventArgs)
12 {
13 //Start the drag
14 sender.CaptureMouse();
15 lastPoint = mouseEventArgs.GetPosition(null);
16 dragging=true;
17 }
18
19 function onMouseMove(sender,mouseEventArgs)
20 {
21 if(dragging)
22 {
23 //Move the element and remember this position for next time
24 var point=mouseEventArgs.GetPosition(null);
25 sender["Canvas.Left"]+=point.X-lastPoint.X;
26 sender["Canvas.Top"]+=point.Y-lastPoint.Y;
27 lastPoint=point;
28 }
29 }
30
31 function onMouseLeftButtonUp(sender,mouseEventArgs)
32 {
33 //Here is the "drop" part of drag-and-drop
34 sender.ReleaseMouseCapture();
35 dragging=false;
36 }
37 }
把这段js脚本放到任何项目中,它都不会与现有函数或全局变量冲突