📅  最后修改于: 2020-11-27 01:46:51             🧑  作者: Mango
jQuery UI draggable()方法用于使任何DOM元素可拖动。将元素设置为可拖动后,可以通过单击鼠标将其移动并将其拖动到视口内的任何位置。
句法:
您可以两种形式使用draggable()方法:
1. $(selector, context).draggable (options) Method
2. $(selector, context).draggable ("action", params) Method
draggable(选项)方法指定可以在HTML页面中移动HTML元素。在这里,option参数指定了所涉及元素的行为。
句法:
$(selector, context).draggable(options);
您可以使用JavaScript对象一次使用一个或多个选项。如果有多个选项,则应使用逗号分隔。例如:
$(selector, context).draggable({option1: value1, option2: value2..... });
以下是可与该方法一起使用的不同选项的列表:
Option | Description |
---|---|
addclasses | If this option is set to false, it will prevent the UI-draggable class from being added in the list of selected DOM elements. By default its value is true. |
appendto | It specifies the element in which the draggable helper should be appended to while dragging. By default its value is “parent”. |
axis | This option constrains dragging to either the horizontal (x) or vertical (y) axis. Its possible values are:”x”, “y”. |
cancel | This option is used to prevent dragging from starting on specified elements. By default its value is “input,textarea, button,select,option”. |
connecttosortable | This option is used to specify a list whose elements are interchangeable. At the end of placement, the element is part of the list. By default its value is “false”. |
containment | Constrains dragging to within the bounds of the specified element or region. By default its value is “false”. |
cursor | It is used to specify the CSS property of the cursor when the element moves. It represents the shape of the mouse pointer. By default its value is “auto”. |
cursorat | It sets the offset of the dragging helper relative to the mouse cursor. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }. By default its value is “false”. |
delay | It specifies the delay in milliseconds, after which the first movement of the mouse is taken into account. The displacement may begin after that time. By default its value is “0”. |
disabled | It disables the ability to move items when set to true. Items cannot be moved until this function is enabled (using the draggable (“enable”) instruction). By default its value is “false”. |
distance | The number of pixels that the mouse must be moved before the displacement is taken into account. By default its value is “1”. |
grid | It snaps the dragging helper to a grid, every x and y pixels. The array must be of the form [ x, y ]. By default its value is “false”. |
handle | If specified, restricts dragging from starting unless the mousedown occurs on the specified element(s). By default its value is “false”. |
helper | It allows for a helper element to be used for dragging display. By default its value is “original”. |
iframefix | It prevents iframes from capturing the mousemove events during a drag. By default its value is “false”. |
opacity | Opacity of the element moved when moving. By default its value is “false”. |
refreshpositions | If set to true, all droppable positions are calculated on every mousemove. By default its value is “false”. |
revert | It indicates whether the element is moved back to its original position at the end of the move. By default its value is “false”. |
revertduration | It indicates the duration of displacement (in milliseconds) after which the element returns to its original position (see options.revert). By default its value is “500”. |
scope | It is used to group sets of draggable and droppable items, in addition to droppable’s accept option. By default its value is “default”. |
scroll | when set to true (the default), the display will scroll if the item is moved outside the viewable area of the window. by default its value is “true”. |
scrollsenstivity | It indicates how many pixels the mouse must exit the window to cause scrolling of the display. By default its value is “20”. |
scrollspeed | It indicates the scrolling speed of the display once scrolling begins. By default its value is “20”. |
snap | It adjusts the display of the item being moved on other elements (which are flown). By default its value is “false”. |
snapmode | It specifies how the adjustment should be made between the moved element and those indicated in options.snap. By default its value is “both”. |
snaptolerance | It specifies the maximum number of pixels in the difference in position necessary to establish the adjustment. By default its value is “20”. |
stack | It controls the z-index of the set of elements that match the selector, always brings the currently dragged item to the front. It is very useful in things like window managers. By default its value is “false”. |
zindex | z-index for the helper while being dragged. By default its value is “false”. |
让我们以一个示例来演示可拖动功能,该功能不会将任何参数传递给draggable()方法。