您的位置:首页 > 产品设计 > UI/UE

silverlight之任意UI元素上启用拖放功能

jebeljebel 2011-07-12 10:30 10 查看
function dragDropEnable(element)
{
//Attach three "private" event handlers contained inside this function
element.AddEventListener("MouseLeftButtonDown",onMouseLeftButtonDown);
element.AddEventListener("MouseMove",onMouseMove);
element.AddEventListener("MouseLeftButtonUp",onMouseLeftButtonUp);

var dragging=false;
var lastPoint=null;

function onMouseLeftButtonDown(sender,mouseEventArgs)
{
//Start the drag
sender.CaptureMouse();
lastPoint = mouseEventArgs.GetPosition(null);
dragging=true;
}

function onMouseMove(sender,mouseEventArgs)
{
if(dragging)
{
//Move the element and remember this position for next time
var point=mouseEventArgs.GetPosition(null);
sender["Canvas.Left"]+=point.X-lastPoint.X;
sender["Canvas.Top"]+=point.Y-lastPoint.Y;
lastPoint=point;
}
}

function onMouseLeftButtonUp(sender,mouseEventArgs)
{
//Here is the "drop" part of drag-and-drop
sender.ReleaseMouseCapture();
dragging=false;
}
}
[/code]
把这段js脚本放到任何项目中,它都不会与现有函数或全局变量冲突

                                            
标签: 
相关文章推荐