Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI Examples - Draggable

jQuery UI Examples - Draggable

Allows elements to be moved with the mouse.

For more details about the draggable interaction, see the API documentation Draggable Widget。

Default functionality

Enables draggable functionality on any DOM element. Move the draggable object by clicking with the mouse and dragging it in the viewport.









  

  jQuery UI 拖动(Draggable) - 默认功能

  

  

  

  

  

  





 

请拖动我!

Auto scroll

Automatically scrolls the document when the draggable is moved outside the viewport. Set the scroll option to true to enable auto scrolling, fine-tune when scrolling is triggered, and set the scroll speed through the scrollSensitivity and scrollSpeed option.









  

  jQuery UI 拖动(Draggable) - 自动滚动

  

  

  

  

  

  





 

Scroll 设置为 true,默认设置

scrollSensitivity 设置为 100

scrollSpeed 设置为 100

Constrain movement

Constrains the movement of each draggable by defining the boundaries of the draggable area. Set the axis option to restrict the draggable's path to the x-axis or y-axis, or use the containment option to specify a parent DOM element or a jQuery selector, such as 'document.'









  

  jQuery UI 拖动(Draggable) - 约束运动

  

  

  

  

  

  





 

沿着轴约束运动:

只能垂直拖拽

只能水平拖拽

或者在另一个 DOM 元素中约束运动:

我被约束在盒子里

我被约束在父元素内

Cursor style

Positions the cursor when dragging an object. By default, the cursor appears in the middle of the dragged object. Use the cursorAt option to specify a different position relative to the draggable (give a pixel value relative to top, right, bottom, or left). Customize the cursor's appearance by providing a cursor option with a valid CSS cursor value. Valid CSS cursor values include: default, move, pointer, crosshair, and so on.









  

  jQuery UI 拖动(Draggable) - 光标样式

  

  

  

  

  

  





 

我总是在中间(相对于鼠标)

我的光标是在 left -5 和 top -5

我的光标位置只控制了 'bottom' 值

Delayed start

Pass delay option sets the number of milliseconds to delay before starting the drag. Through the distance option, dragging is allowed only after the cursor is pressed and has moved the specified number of pixels.









  

  jQuery UI 拖动(Draggable) - 延迟开始

  

  

  

  

  

  





 

只有把我拖拽了 20 像素后,拖拽才开始

不管 distance 是多少,您都必须拖拽并等待 1000ms 后拖拽才开始

Events

on the draggable start、drag and stop event. Triggered when dragging starts, the start event is triggered during dragging, and the drag event is triggered when dragging stops. stop events.









  

  jQuery UI 拖动(Draggable) - 事件

  

  

  

  

  

  





 

请拖拽我,触发一连串的事件。

  • "start" 被调用 0x
  • "drag" 被调用 0x
  • "stop" 被调用 0x

Handles

Allows dragging only when the cursor is over a specified part of the draggable. Use the handle option to specify a jQuery selector for the element (or group of elements) used to drag the object.

Or disallows dragging when the cursor is over a specified element (or group of elements) inside the draggable. Use the cancel option to specify a jQuery selector that cancels the drag functionality.









  

  jQuery UI 拖动(Draggable) - Handles

  

  

  

  

  

  





 

您只可以在这个范围内拖拽我

您可以把我向四周拖拽…

…但是您不可以在这个范围内拖拽我

Revert position

When a draggable with the boolean revert option set stops dragging, returns the draggable (or its helper) to its original position.









  

  jQuery UI 拖动(Draggable) - 还原位置

  

  

  

  

  

  





 

还原

还原助手

Snap to element or grid

Snaps the draggable to the inner or outer boundaries of a DOM element. Use the snap、snapMode(inner, outer, both) and the snapTolerance(the distance in pixels between the draggable and the element when snapping) option.

Or snaps the draggable to a grid. Set the grid option to set the size of the grid cells (height or width in pixels).









  

  jQuery UI 拖动(Draggable) - 对齐到元素或网格

  

  

  

  

  

  





 

我是对齐目标


默认(snap: true),对齐到所有其他的 draggable 元素

我只对齐到大盒子

我只对齐到大盒子的外边缘

我对齐到一个 20 x 20 网格

我对齐到一个 80 x 80 网格

Visual feedback

Provides feedback to the user by dragging an object as a helper.helper option accepts the value 'original' (move the draggable object with the cursor), 'clone' (move a copy of the draggable with the cursor), or a function returning a DOM element (shown near the cursor during the drag). Through the opacity option, the helper's opacity is controlled.

To distinguish which draggable is being dragged, keep the moving draggable in front. While dragging, use the zIndex option to set a high z-index for the helper, or use the stack option to ensure that when dragging stops, the last dragged item always appears above the other items in the same group.









  

  jQuery UI 拖动(Draggable) - 视觉反馈

  

  

  

  

  

  





 

助手:

原始的

半透明的克隆

自定义助手(与 cursorAt 结合)

堆叠:

我们是 draggables..

..它的 z-index 是自动控制的..

..带有 stack 选项。

jQuery UI Draggable + Sortable

Seamless interaction between Draggable and Sortable.









  

  jQuery UI 拖动(Draggable) + 排序(Sortable)

  

  

  

  

  

  





 

  • 请拖拽我
  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5