拖动(Draggable)


jQuery UI 实例 - 拖动(Draggable)

允许使用鼠标移动元素。

如需了解更多有关 draggable 交互的细节,请查看 API 文档 可拖拽小部件(Draggable Widget)。

默认功能

在任意的 DOM 元素上启用 draggable 功能。通过鼠标点击并在视区中拖动来移动 draggable 对象。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 默认功能</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable { width: 150px; height: 150px; padding: 0.5em; }
  12.   </style>
  13.   <script>
  14.   $(function() {
  15.     $( "#draggable" ).draggable();
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <div id="draggable" class="ui-widget-content">
  22.   <p>请拖动我!</p>
  23. </div>
  24.  
  25.  
  26. </body>
  27. </html>

自动滚动

当 draggable 移动到视区外时自动滚动文档。设置 scroll 选项为 true 来启用自动滚动,当滚动触发时进行微调,滚动速度是通过 scrollSensitivityscrollSpeed 选项设置的。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 自动滚动</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2, #draggable3 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   </style>
  13.   <script>
  14.   $(function() {
  15.     $( "#draggable" ).draggable({ scroll: true });
  16.     $( "#draggable2" ).draggable({ scroll: true, scrollSensitivity: 100 });
  17.     $( "#draggable3" ).draggable({ scroll: true, scrollSpeed: 100 });
  18.   });
  19.   </script>
  20. </head>
  21. <body>
  22.  
  23. <div id="draggable" class="ui-widget-content">
  24.   <p>Scroll 设置为 true,默认设置</p>
  25. </div>
  26.  
  27. <div id="draggable2" class="ui-widget-content">
  28.   <p>scrollSensitivity 设置为 100</p>
  29. </div>
  30.  
  31. <div id="draggable3" class="ui-widget-content">
  32.   <p>scrollSpeed 设置为 100</p>
  33. </div>
  34.  
  35. <div style="height: 5000px; width: 1px;"></div>
  36.  
  37.  
  38. </body>
  39. </html>

约束运动

通过定义 draggable 区域的边界来约束每个 draggable 的运动。设置 axis 选项来限制 draggable 的路径为 x 轴或者 y 轴,或者使用 containment 选项来指定一个父级的 DOM 元素或者一个 jQuery 选择器,比如 'document.'。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 约束运动</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   .draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   #draggable, #draggable2 { margin-bottom:20px; }
  13.   #draggable { cursor: n-resize; }
  14.   #draggable2 { cursor: e-resize; }
  15.   #containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; }
  16.   h3 { clear: left; }
  17.   </style>
  18.   <script>
  19.   $(function() {
  20.     $( "#draggable" ).draggable({ axis: "y" });
  21.     $( "#draggable2" ).draggable({ axis: "x" });
  22.  
  23.     $( "#draggable3" ).draggable({ containment: "#containment-wrapper", scroll: false });
  24.     $( "#draggable5" ).draggable({ containment: "parent" });
  25.   });
  26.   </script>
  27. </head>
  28. <body>
  29.  
  30. <h3>沿着轴约束运动:</h3>
  31.  
  32. <div id="draggable" class="draggable ui-widget-content">
  33.   <p>只能垂直拖拽</p>
  34. </div>
  35.  
  36. <div id="draggable2" class="draggable ui-widget-content">
  37.   <p>只能水平拖拽</p>
  38. </div>
  39.  
  40. <h3>或者在另一个 DOM 元素中约束运动:</h3>
  41. <div id="containment-wrapper">
  42.   <div id="draggable3" class="draggable ui-widget-content">
  43.     <p>我被约束在盒子里</p>
  44.   </div>
  45.  
  46.   <div class="draggable ui-widget-content">
  47.     <p id="draggable5" class="ui-widget-header">我被约束在父元素内</p>
  48.   </div>
  49. </div>
  50.  
  51.  
  52. </body>
  53. </html>

光标样式

当拖拽对象时定位光标。默认情况下,光标是出现在被拖拽对象的中间。使用 cursorAt 选项来指定相对于 draggable 的另一个位置(指定一个相对于 top、right、bottom、left 的像素值)。通过提供一个带有有效的 CSS 光标值的 cursor 选项,来自定义光标的外观。有效的 CSS 光标值包括:default、move、pointer、crosshair,等等。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 光标样式</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2, #draggable3 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   </style>
  13.   <script>
  14.   $(function() {
  15.     $( "#draggable" ).draggable({ cursor: "move", cursorAt: { top: 56, left: 56 } });
  16.     $( "#draggable2" ).draggable({ cursor: "crosshair", cursorAt: { top: -5, left: -5 } });
  17.     $( "#draggable3" ).draggable({ cursorAt: { bottom: 0 } });
  18.   });
  19.   </script>
  20. </head>
  21. <body>
  22.  
  23. <div id="draggable" class="ui-widget-content">
  24.   <p>我总是在中间(相对于鼠标)</p>
  25. </div>
  26.  
  27. <div id="draggable2" class="ui-widget-content">
  28.   <p>我的光标是在 left -5 和 top -5</p>
  29. </div>
  30.  
  31. <div id="draggable3" class="ui-widget-content">
  32.   <p>我的光标位置只控制了 'bottom' 值</p>
  33. </div>
  34.  
  35.  
  36. </body>
  37. </html>

延迟开始

通过 delay 选项设置延迟开始拖拽的毫秒数。通过 distance 选项设置光标被按下且拖拽指定像素后才允许拖拽。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 延迟开始</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2 { width: 120px; height: 120px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   </style>
  13.   <script>
  14.   $(function() {
  15.     $( "#draggable" ).draggable({ distance: 20 });
  16.     $( "#draggable2" ).draggable({ delay: 1000 });
  17.     $( ".ui-draggable" ).disableSelection();
  18.   });
  19.   </script>
  20. </head>
  21. <body>
  22.  
  23. <div id="draggable" class="ui-widget-content">
  24.   <p>只有把我拖拽了 20 像素后,拖拽才开始</p>
  25. </div>
  26.  
  27. <div id="draggable2" class="ui-widget-content">
  28.   <p>不管 distance 是多少,您都必须拖拽并等待 1000ms 后拖拽才开始</p>
  29. </div>
  30.  
  31.  
  32. </body>
  33. </html>

事件

draggable 上的 startdragstop 事件。拖拽开始时触发 start 事件,拖拽期间触发 drag 事件,拖拽停止时触发 stop 事件。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 事件</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable { width: 16em; padding: 0 1em; }
  12.   #draggable ul li { margin: 1em 0; padding: 0.5em 0; } * html #draggable ul li { height: 1%; }
  13.   #draggable ul li span.ui-icon { float: left; }
  14.   #draggable ul li span.count { font-weight: bold; }
  15.   </style>
  16.   <script>
  17.   $(function() {
  18.     var $start_counter = $( "#event-start" ),
  19.       $drag_counter = $( "#event-drag" ),
  20.       $stop_counter = $( "#event-stop" ),
  21.       counts = [ 0, 0, 0 ];
  22.  
  23.     $( "#draggable" ).draggable({
  24.       start: function() {
  25.         counts[ 0 ]++;
  26.         updateCounterStatus( $start_counter, counts[ 0 ] );
  27.       },
  28.       drag: function() {
  29.         counts[ 1 ]++;
  30.         updateCounterStatus( $drag_counter, counts[ 1 ] );
  31.       },
  32.       stop: function() {
  33.         counts[ 2 ]++;
  34.         updateCounterStatus( $stop_counter, counts[ 2 ] );
  35.       }
  36.     });
  37.  
  38.     function updateCounterStatus( $event_counter, new_count ) {
  39.       // 首先更新视觉状态...
  40.       if ( !$event_counter.hasClass( "ui-state-hover" ) ) {
  41.         $event_counter.addClass( "ui-state-hover" )
  42.           .siblings().removeClass( "ui-state-hover" );
  43.       }
  44.       // ...然后更新数字
  45.       $( "span.count", $event_counter ).text( new_count );
  46.     }
  47.   });
  48.   </script>
  49. </head>
  50. <body>
  51.  
  52. <div id="draggable" class="ui-widget ui-widget-content">
  53.  
  54.   <p>请拖拽我,触发一连串的事件。</p>
  55.  
  56.   <ul class="ui-helper-reset">
  57.     <li id="event-start" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-play"></span>"start" 被调用 <span class="count">0</span>x</li>
  58.     <li id="event-drag" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-arrow-4"></span>"drag" 被调用 <span class="count">0</span>x</li>
  59.     <li id="event-stop" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-stop"></span>"stop" 被调用 <span class="count">0</span>x</li>
  60.   </ul>
  61. </div>
  62.  
  63.  
  64. </body>
  65. </html>

Handles

只有当光标在 draggable 上指定部分时才允许拖拽。使用 handle 选项来指定用于拖拽对象的元素(或元素组)的 jQuery 选择器。

Or prevent dragging when the cursor is over a specific element (or group of elements) within the draggable. Use the cancel option to specify a jQuery selector over which to "cancel" draggable functionality.

或者当光标在 draggable 内指定元素(或元素组)上时不允许拖拽。使用 handle 选项来指定取消拖拽功能的 jQuery 选择器。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - Handles</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   #draggable p { cursor: move; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#draggable" ).draggable({ handle: "p" });
  17.     $( "#draggable2" ).draggable({ cancel: "p.ui-widget-header" });
  18.     $( "div, p" ).disableSelection();
  19.   });
  20.   </script>
  21. </head>
  22. <body>
  23.  
  24. <div id="draggable" class="ui-widget-content">
  25.   <p class="ui-widget-header">您只可以在这个范围内拖拽我</p>
  26. </div>
  27.  
  28. <div id="draggable2" class="ui-widget-content">
  29.   <p>您可以把我向四周拖拽&hellip;</p>
  30.   <p class="ui-widget-header">&hellip;但是您不可以在这个范围内拖拽我</p>
  31. </div>
  32.  
  33.  
  34. </body>
  35. </html>

还原位置

当带有布尔值 revert 选项的 draggable 停止拖拽时,返回 draggable(或它的助手)到原始位置。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 还原位置</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   </style>
  13.   <script>
  14.   $(function() {
  15.     $( "#draggable" ).draggable({ revert: true });
  16.     $( "#draggable2" ).draggable({ revert: true, helper: "clone" });
  17.   });
  18.   </script>
  19. </head>
  20. <body>
  21.  
  22. <div id="draggable" class="ui-widget-content">
  23.   <p>还原</p>
  24. </div>
  25.  
  26. <div id="draggable2" class="ui-widget-content">
  27.   <p>还原助手</p>
  28. </div>
  29.  
  30.  
  31. </body>
  32. </html>

对齐到元素或网格

对齐 draggable 到 DOM 元素的内部或外部边界。使用 snapsnapMode(inner, outer, both)和 snapTolerance(当调用对齐时,draggable 与元素之间的距离,以像素为单位)选项。

或者对齐 draggable 到网格。通过 grid 选项设置网格单元的尺寸(以像素为单位的高度或宽度)。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 对齐到元素或网格</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   .draggable { width: 90px; height: 80px; padding: 5px; float: left; margin: 0 10px 10px 0; font-size: .9em; }
  12.   .ui-widget-header p, .ui-widget-content p { margin: 0; }
  13.   #snaptarget { height: 140px; }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     $( "#draggable" ).draggable({ snap: true });
  18.     $( "#draggable2" ).draggable({ snap: ".ui-widget-header" });
  19.     $( "#draggable3" ).draggable({ snap: ".ui-widget-header", snapMode: "outer" });
  20.     $( "#draggable4" ).draggable({ grid: [ 20, 20 ] });
  21.     $( "#draggable5" ).draggable({ grid: [ 80, 80 ] });
  22.   });
  23.   </script>
  24. </head>
  25. <body>
  26.  
  27. <div id="snaptarget" class="ui-widget-header">
  28.   <p>我是对齐目标</p>
  29. </div>
  30.  
  31. <br style="clear:both">
  32.  
  33. <div id="draggable" class="draggable ui-widget-content">
  34.   <p>默认(snap: true),对齐到所有其他的 draggable 元素</p>
  35. </div>
  36.  
  37. <div id="draggable2" class="draggable ui-widget-content">
  38.   <p>我只对齐到大盒子</p>
  39. </div>
  40.  
  41. <div id="draggable3" class="draggable ui-widget-content">
  42.   <p>我只对齐到大盒子的外边缘</p>
  43. </div>
  44.  
  45. <div id="draggable4" class="draggable ui-widget-content">
  46.   <p>我对齐到一个 20 x 20 网格</p>
  47. </div>
  48.  
  49. <div id="draggable5" class="draggable ui-widget-content">
  50.   <p>我对齐到一个 80 x 80 网格</p>
  51. </div>
  52.  
  53.  
  54. </body>
  55. </html>

视觉反馈

给用户提供反馈,就像以助手方式拖拽对象一样。helper 选项接受值 'original'(用光标移动 draggable 对象),'clone'(用光标移动 draggable 的副本),或者一个返回 DOM 元素的函数(该元素在拖拽期间显示在光标附近)。通过 opacity 选项控制助手的透明度。

为了区别哪一个 draggable 正在被拖拽,让在运动中的 draggable 保持最前。如果正在拖拽,使用 zIndex 选项来设置助手的高度 z-index,或者使用 stack 选项来确保当停止拖拽时,最后一个被拖拽的项目总是出现在同组其他项目的上面。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) - 视觉反馈</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #draggable, #draggable2, #draggable3, #set div { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  12.   #draggable, #draggable2, #draggable3 { margin-bottom:20px; }
  13.   #set { clear:both; float:left; width: 368px; height: 120px; }
  14.   p { clear:both; margin:0; padding:1em 0; }
  15.   </style>
  16.   <script>
  17.   $(function() {
  18.     $( "#draggable" ).draggable({ helper: "original" });
  19.     $( "#draggable2" ).draggable({ opacity: 0.7, helper: "clone" });
  20.     $( "#draggable3" ).draggable({
  21.       cursor: "move",
  22.       cursorAt: { top: -12, left: -20 },
  23.       helper: function( event ) {
  24.         return $( "<div class='ui-widget-header'>I'm a custom helper</div>" );
  25.       }
  26.     });
  27.     $( "#set div" ).draggable({ stack: "#set div" });
  28.   });
  29.   </script>
  30. </head>
  31. <body>
  32.  
  33. <h3 class="docs">助手:</h3>
  34.  
  35. <div id="draggable" class="ui-widget-content">
  36.   <p>原始的</p>
  37. </div>
  38.  
  39. <div id="draggable2" class="ui-widget-content">
  40.   <p>半透明的克隆</p>
  41. </div>
  42.  
  43. <div id="draggable3" class="ui-widget-content">
  44.   <p>自定义助手(与 cursorAt 结合)</p>
  45. </div>
  46.  
  47. <h3 class="docs">堆叠:</h3>
  48. <div id="set">
  49.   <div class="ui-widget-content">
  50.     <p>我们是 draggables..</p>
  51.   </div>
  52.  
  53.   <div class="ui-widget-content">
  54.     <p>..它的 z-index 是自动控制的..</p>
  55.   </div>
  56.  
  57.   <div class="ui-widget-content">
  58.     <p>..带有 stack 选项。</p>
  59.   </div>
  60. </div>
  61.  
  62.  
  63. </body>
  64. </html>

jQuery UI Draggable + Sortable

Draggable 与 Sortable 的无缝交互。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 拖动(Draggable) + 排序(Sortable)</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   ul { list-style-type: none; margin: 0; padding: 0; margin-bottom: 10px; }
  12.   li { margin: 5px; padding: 5px; width: 150px; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#sortable" ).sortable({
  17.       revert: true
  18.     });
  19.     $( "#draggable" ).draggable({
  20.       connectToSortable: "#sortable",
  21.       helper: "clone",
  22.       revert: "invalid"
  23.     });
  24.     $( "ul, li" ).disableSelection();
  25.   });
  26.   </script>
  27. </head>
  28. <body>
  29.  
  30. <ul>
  31.   <li id="draggable" class="ui-state-highlight">请拖拽我</li>
  32. </ul>
  33.  
  34. <ul id="sortable">
  35.   <li class="ui-state-default">Item 1</li>
  36.   <li class="ui-state-default">Item 2</li>
  37.   <li class="ui-state-default">Item 3</li>
  38.   <li class="ui-state-default">Item 4</li>
  39.   <li class="ui-state-default">Item 5</li>
  40. </ul>
  41.  
  42.  
  43. </body>
  44. </html>