课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

拖动(Draggable)

当前位置:免费教程 » JS/JS库/框架 » jQuery UI

允许使用鼠标移动元素。

如需了解更多有关 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. <div id="draggable" class="ui-widget-content">
  21. <p>请拖动我!</p>
  22. </div>
  23. </body>
  24. </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. <div id="draggable" class="ui-widget-content">
  23. <p>Scroll 设置为 true,默认设置</p>
  24. </div>
  25. <div id="draggable2" class="ui-widget-content">
  26. <p>scrollSensitivity 设置为 100</p>
  27. </div>
  28. <div id="draggable3" class="ui-widget-content">
  29. <p>scrollSpeed 设置为 100</p>
  30. </div>
  31. <div style="height: 5000px; width: 1px;"></div>
  32. </body>
  33. </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. $( "#draggable3" ).draggable({ containment: "#containment-wrapper", scroll: false });
  23. $( "#draggable5" ).draggable({ containment: "parent" });
  24. });
  25. </script>
  26. </head>
  27. <body>
  28. <h3>沿着轴约束运动:</h3>
  29. <div id="draggable" class="draggable ui-widget-content">
  30. <p>只能垂直拖拽</p>
  31. </div>
  32. <div id="draggable2" class="draggable ui-widget-content">
  33. <p>只能水平拖拽</p>
  34. </div>
  35. <h3>或者在另一个 DOM 元素中约束运动:</h3>
  36. <div id="containment-wrapper">
  37. <div id="draggable3" class="draggable ui-widget-content">
  38. <p>我被约束在盒子里</p>
  39. </div>
  40. <div class="draggable ui-widget-content">
  41. <p id="draggable5" class="ui-widget-header">我被约束在父元素内</p>
  42. </div>
  43. </div>
  44. </body>
  45. </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. <div id="draggable" class="ui-widget-content">
  23. <p>我总是在中间(相对于鼠标)</p>
  24. </div>
  25. <div id="draggable2" class="ui-widget-content">
  26. <p>我的光标是在 left -5 和 top -5</p>
  27. </div>
  28. <div id="draggable3" class="ui-widget-content">
  29. <p>我的光标位置只控制了 'bottom' 值</p>
  30. </div>
  31. </body>
  32. </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. <div id="draggable" class="ui-widget-content">
  23. <p>只有把我拖拽了 20 像素后,拖拽才开始</p>
  24. </div>
  25. <div id="draggable2" class="ui-widget-content">
  26. <p>不管 distance 是多少,您都必须拖拽并等待 1000ms 后拖拽才开始</p>
  27. </div>
  28. </body>
  29. </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. $( "#draggable" ).draggable({
  23. start: function() {
  24. counts[ 0 ]++;
  25. updateCounterStatus( $start_counter, counts[ 0 ] );
  26. },
  27. drag: function() {
  28. counts[ 1 ]++;
  29. updateCounterStatus( $drag_counter, counts[ 1 ] );
  30. },
  31. stop: function() {
  32. counts[ 2 ]++;
  33. updateCounterStatus( $stop_counter, counts[ 2 ] );
  34. }
  35. });
  36. function updateCounterStatus( $event_counter, new_count ) {
  37. // 首先更新视觉状态...
  38. if ( !$event_counter.hasClass( "ui-state-hover" ) ) {
  39. $event_counter.addClass( "ui-state-hover" )
  40. .siblings().removeClass( "ui-state-hover" );
  41. }
  42. // ...然后更新数字
  43. $( "span.count", $event_counter ).text( new_count );
  44. }
  45. });
  46. </script>
  47. </head>
  48. <body>
  49. <div id="draggable" class="ui-widget ui-widget-content">
  50. <p>请拖拽我,触发一连串的事件。</p>
  51. <ul class="ui-helper-reset">
  52. <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>
  53. <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>
  54. <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>
  55. </ul>
  56. </div>
  57. </body>
  58. </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. <div id="draggable" class="ui-widget-content">
  24. <p class="ui-widget-header">您只可以在这个范围内拖拽我</p>
  25. </div>
  26. <div id="draggable2" class="ui-widget-content">
  27. <p>您可以把我向四周拖拽&hellip;</p>
  28. <p class="ui-widget-header">&hellip;但是您不可以在这个范围内拖拽我</p>
  29. </div>
  30. </body>
  31. </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. <div id="draggable" class="ui-widget-content">
  22. <p>还原</p>
  23. </div>
  24. <div id="draggable2" class="ui-widget-content">
  25. <p>还原助手</p>
  26. </div>
  27. </body>
  28. </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. <div id="snaptarget" class="ui-widget-header">
  27. <p>我是对齐目标</p>
  28. </div>
  29. <br style="clear:both">
  30. <div id="draggable" class="draggable ui-widget-content">
  31. <p>默认(snap: true),对齐到所有其他的 draggable 元素</p>
  32. </div>
  33. <div id="draggable2" class="draggable ui-widget-content">
  34. <p>我只对齐到大盒子</p>
  35. </div>
  36. <div id="draggable3" class="draggable ui-widget-content">
  37. <p>我只对齐到大盒子的外边缘</p>
  38. </div>
  39. <div id="draggable4" class="draggable ui-widget-content">
  40. <p>我对齐到一个 20 x 20 网格</p>
  41. </div>
  42. <div id="draggable5" class="draggable ui-widget-content">
  43. <p>我对齐到一个 80 x 80 网格</p>
  44. </div>
  45. </body>
  46. </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. <h3 class="docs">助手:</h3>
  33. <div id="draggable" class="ui-widget-content">
  34. <p>原始的</p>
  35. </div>
  36. <div id="draggable2" class="ui-widget-content">
  37. <p>半透明的克隆</p>
  38. </div>
  39. <div id="draggable3" class="ui-widget-content">
  40. <p>自定义助手(与 cursorAt 结合)</p>
  41. </div>
  42. <h3 class="docs">堆叠:</h3>
  43. <div id="set">
  44. <div class="ui-widget-content">
  45. <p>我们是 draggables..</p>
  46. </div>
  47. <div class="ui-widget-content">
  48. <p>..它的 z-index 是自动控制的..</p>
  49. </div>
  50. <div class="ui-widget-content">
  51. <p>..带有 stack 选项。</p>
  52. </div>
  53. </div>
  54. </body>
  55. </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. <ul>
  30. <li id="draggable" class="ui-state-highlight">请拖拽我</li>
  31. </ul>
  32. <ul id="sortable">
  33. <li class="ui-state-default">Item 1</li>
  34. <li class="ui-state-default">Item 2</li>
  35. <li class="ui-state-default">Item 3</li>
  36. <li class="ui-state-default">Item 4</li>
  37. <li class="ui-state-default">Item 5</li>
  38. </ul>
  39. </body>
  40. </html>

我要试一下

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号