Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

拖拽

已废弃 Gecko 1.9.1 (Firefox 3.5 / Thunderbird 3.0 / SeaMonkey 2.0)
该特性已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。

自 Gecko 1.9.1 (Firefox 3.5)起,此API已被官方正式弃用。更新的、更简单、更便捷(portable)的API应该被用来替换此API(should be used in their place)。

这部分描述了怎样实现可以拖拽和拖放到其他对象上的可拖拽对象。

拖拽和拖放接口

很多用户界面都允许用户在界面内拖拽某些具体的对象(Many user interfaces allow one to drag particular objects around within the interface)。举个例子来说,拖拽一些文件到其他目录下;或者以拖拽一个图标到另一个窗口的方式来打开这个图标所指代的文档。Mozilla 和 XUL 提供一些事件来处理用户试图拖拽对象时的状态。

用户以按住鼠标然后移动鼠标的方式开始拖拽。当用户释放鼠标时,拖拽过程停止。事件处理器可以在拖拽开始时、停止时、还有拖拽过程中间时被调用。

Mozilla 通过拖拽会话(drag session)实现了拖拽。当用户要求拖拽某个可拖拽对象时,开始一个拖拽会话(drag session)。由拖拽会话(drag session)更新鼠标指针的位置,并处理可拖拽对象最后拖放的位置。如果一个对象不可拖拽,则不会开始一个拖拽会话(drag session)。由于用户通常只有一个鼠标,所以同一时刻只有一个拖拽会话(drag session)处于使用状态。

注意,拖拽会话(drag session)可以由Mozilla本身或其他应用创建。Mozilla会转换需要的拖拽数据(will translate the data being dragged as needed)。

下面的列表描述了可以被调用的事件处理器,这些处理器可以绑定在任意元素上。You only need to put values for the handlers where you need to do something when the event occurs.

ondrag
周期性地调用,贯穿整个拖拽和拖放全程。
ondraggesture 
当用户开始拖拽一个元素时被调用,确切地说是当用户按住鼠标按键并移动鼠标时。The script in this handler should set up a drag session.
ondragstart  
An alias for ondraggesture; this is the HTML 5 spec name for the event and may be used in HTML or XUL; however, for backward compatibility with older versions of Firefox, you may wish to continue using ondraggesture in XUL.
ondragover 
This event handler is called for an element when something is being dragged over top of it. If the object can be dropped on the element, the drag session should be notified.
ondragenter 
Called for an element when the mouse pointer first moves over the element while something is being dragged. This might be used to change the appearance of the element to indicate to the user that the object can be dropped on it.
ondragexit 
Called for an element when the mouse pointer moves out of an element while something is being dragged. The is also called after a drop is complete so that an element has a chance to remove any highlighting or other indication.
ondragdrop 
This event handler is called for an element when something is dropped on the element. At this point, the user has already released the mouse button. The element can simply ignore the event or can handle it some way, such as pasting the dragged object into itself.
ondragend  
Called when the drag operation is finished.

There are two ways that drag and drop events can be handled. This first involves using the drag and drop XPCOM interfaces directly. The second is to use a JavaScript wrapper object that handles some of this for you. The code for this wrapper can be found in a file named toolkit/content/nsDragAndDrop.js nsDragAndDrop.js which is contained in the widget-toolkit (or global) package.

XPCOM Drag and Drop interfaces

Two interfaces are used to support drag and drop. The first is a drag service, nsIDragService and the second is the drag session, nsIDragSession.

The nsIDragService is responsible for creating drag sessions when a drag starts, and removing the drag session when the drag is complete. The function invokeDragSession should be called to start a drag inside an ondraggesture event handler. Once this function is called, a drag has started.

The function invokeDragSession takes four parameters, as described below:

invokeDragSession(element,transferableArray,region,actions)
element 
A reference to the element that is being dragged. This can be retrieved by getting the property event.target during the event handler.
transferableArray 
An array of nsITransferable objects, one for each item being dragged. An array is used because you might want to drag several objects at once, such as a set of files.
region 
A region used for feedback indication. This should usually be set to null.
actions 
The actions that the drag uses. This should be set to one of the following constants, or several added together. The action can be changed during the drag depending on what is being dragged over.
nsIDragService.DRAGDROP_ACTION_NONE 
Used to indicate that no drag is valid.
nsIDragService.DRAGDROP_ACTION_COPY 
The item being dragged should be copied to its dropped location.
nsIDragService.DRAGDROP_ACTION_MOVE 
The item being dragged should be moved to its dropped location.
nsIDragService.DRAGDROP_ACTION_LINK 
A link (or shortcut or alias) to the item being dragged should be created in the dropped location.

The interface nsIDragService also provides the function getCurrentSession which can be called from within the drag event handlers to get and modify the state of the drag. The function returns an object that implements nsIDragSession.

The interface nsIDragSession is used to get and set properties of the drag that is currently occuring. The following properties and methods are available:

canDrop 
Set this property to true if the element the mouse is currently over can accept the object currently being dragged to be dropped on it. Set the value to false if it doesn't make sense to drop the object on it. This should be changed in the ondragover and ondragenter event handlers.
dragAction 
Set to the current action to be performed, which should be one or more of the constants described earlier. This can be used to provide extra feedback to the user.
numDropItems 
The number of items being dragged. For example, this will be set to 5 if five bookmarks are being dragged.
getData(transfer,index) 
Get the data being dragged. The first argument should be an nsITransferable object to hold the data. The second argument, index, should be the index of the item to return.
sourceDocument 
The document where the drag started.
sourceNode 
The DOM node where the drag started.
isDataFlavorSupported(flavor) 
Returns true if the data being dragged contains data of the specified flavor.

Original Document Information

  •  

文档标签和贡献者

 此页面的贡献者: charlie, ziyunfei, hanyuxinting
 最后编辑者: charlie,