课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

放置(Droppable)

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

为可拖拽小部件创建目标。

如需了解更多有关 droppable 交互的细节,请查看 API 文档 可放置小部件(Droppable Widget)

默认功能

在任意的 DOM 元素上启用 droppable 功能,并为可拖拽小部件创建目标。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 默认功能</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: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  12. #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  13. </style>
  14. <script>
  15. $(function() {
  16. $( "#draggable" ).draggable();
  17. $( "#droppable" ).droppable({
  18. drop: function( event, ui ) {
  19. $( this )
  20. .addClass( "ui-state-highlight" )
  21. .find( "p" )
  22. .html( "Dropped!" );
  23. }
  24. });
  25. });
  26. </script>
  27. </head>
  28. <body>
  29. <div id="draggable" class="ui-widget-content">
  30. <p>请把我拖拽到目标处!</p>
  31. </div>
  32. <div id="droppable" class="ui-widget-header">
  33. <p>请放置在这里!</p>
  34. </div>
  35. </body>
  36. </html>

我要试一下

接受

使用 accept 选项指定目标 droppable 接受哪一个元素(或元素组)。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 接受</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. #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  12. #draggable, #draggable-nonvalid { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  13. </style>
  14. <script>
  15. $(function() {
  16. $( "#draggable, #draggable-nonvalid" ).draggable();
  17. $( "#droppable" ).droppable({
  18. accept: "#draggable",
  19. activeClass: "ui-state-hover",
  20. hoverClass: "ui-state-active",
  21. drop: function( event, ui ) {
  22. $( this )
  23. .addClass( "ui-state-highlight" )
  24. .find( "p" )
  25. .html( "Dropped!" );
  26. }
  27. });
  28. });
  29. </script>
  30. </head>
  31. <body>
  32. <div id="draggable-nonvalid" class="ui-widget-content">
  33. <p>我是不能被放置的 draggable</p>
  34. </div>
  35. <div id="draggable" class="ui-widget-content">
  36. <p>请拖拽我到目标</p>
  37. </div>
  38. <div id="droppable" class="ui-widget-header">
  39. <p>accept: '#draggable'</p>
  40. </div>
  41. </body>
  42. </html>

我要试一下

防止传播

当使用嵌套的 droppable 时 — 例如,您可以有一个树形的可编辑的目录结构,带有文件夹和文档节点 — greedy 选项设置为 true 来防止当 draggable 被放置在子节点(droppable)上时的事件传播。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 防止传播</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: 100px; height: 40px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  12. #droppable, #droppable2 { width: 230px; height: 120px; padding: 0.5em; float: left; margin: 10px; }
  13. #droppable-inner, #droppable2-inner { width: 170px; height: 60px; padding: 0.5em; float: left; margin: 10px; }
  14. </style>
  15. <script>
  16. $(function() {
  17. $( "#draggable" ).draggable();
  18. $( "#droppable, #droppable-inner" ).droppable({
  19. activeClass: "ui-state-hover",
  20. hoverClass: "ui-state-active",
  21. drop: function( event, ui ) {
  22. $( this )
  23. .addClass( "ui-state-highlight" )
  24. .find( "> p" )
  25. .html( "Dropped!" );
  26. return false;
  27. }
  28. });
  29. $( "#droppable2, #droppable2-inner" ).droppable({
  30. greedy: true,
  31. activeClass: "ui-state-hover",
  32. hoverClass: "ui-state-active",
  33. drop: function( event, ui ) {
  34. $( this )
  35. .addClass( "ui-state-highlight" )
  36. .find( "> p" )
  37. .html( "Dropped!" );
  38. }
  39. });
  40. });
  41. </script>
  42. </head>
  43. <body>
  44. <div id="draggable" class="ui-widget-content">
  45. <p>请拖拽我到目标</p>
  46. </div>
  47. <div id="droppable" class="ui-widget-header">
  48. <p>外部 droppable</p>
  49. <div id="droppable-inner" class="ui-widget-header">
  50. <p>内部 droppable(不带有 greedy)</p>
  51. </div>
  52. </div>
  53. <div id="droppable2" class="ui-widget-header">
  54. <p>外部 droppable</p>
  55. <div id="droppable2-inner" class="ui-widget-header">
  56. <p>内部 droppable(带有 greedy)</p>
  57. </div>
  58. </div>
  59. </body>
  60. </html>

我要试一下

还原 draggable 的位置

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

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 还原 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: 10px 10px 10px 0; }
  12. #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  13. </style>
  14. <script>
  15. $(function() {
  16. $( "#draggable" ).draggable({ revert: "valid" });
  17. $( "#draggable2" ).draggable({ revert: "invalid" });
  18. $( "#droppable" ).droppable({
  19. activeClass: "ui-state-default",
  20. hoverClass: "ui-state-hover",
  21. drop: function( event, ui ) {
  22. $( this )
  23. .addClass( "ui-state-highlight" )
  24. .find( "p" )
  25. .html( "已放置!" );
  26. }
  27. });
  28. });
  29. </script>
  30. </head>
  31. <body>
  32. <div id="draggable" class="ui-widget-content">
  33. <p>当被放置在目标上时还原</p>
  34. </div>
  35. <div id="draggable2" class="ui-widget-content">
  36. <p>当未被放置在目标上时还原</p>
  37. </div>
  38. <div id="droppable" class="ui-widget-header">
  39. <p>请放置在这里</p>
  40. </div>
  41. </body>
  42. </html>

我要试一下

购物车演示

演示如何使用折叠面板来展示产品的目录结构,使用拖拽和放置来添加产品到购物车,购物车中的产品是可排序的。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 购物车演示</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. h1 { padding: .2em; margin: 0; }
  12. #products { float:left; width: 500px; margin-right: 2em; }
  13. #cart { width: 200px; float: left; margin-top: 1em; }
  14. /* 定义列表样式,以便最大化 droppable */
  15. #cart ol { margin: 0; padding: 1em 0 1em 3em; }
  16. </style>
  17. <script>
  18. $(function() {
  19. $( "#catalog" ).accordion();
  20. $( "#catalog li" ).draggable({
  21. appendTo: "body",
  22. helper: "clone"
  23. });
  24. $( "#cart ol" ).droppable({
  25. activeClass: "ui-state-default",
  26. hoverClass: "ui-state-hover",
  27. accept: ":not(.ui-sortable-helper)",
  28. drop: function( event, ui ) {
  29. $( this ).find( ".placeholder" ).remove();
  30. $( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
  31. }
  32. }).sortable({
  33. items: "li:not(.placeholder)",
  34. sort: function() {
  35. // 获取由 droppable 与 sortable 交互而加入的条目
  36. // 使用 connectWithSortable 可以解决这个问题,但不允许您自定义 active/hoverClass 选项
  37. $( this ).removeClass( "ui-state-default" );
  38. }
  39. });
  40. });
  41. </script>
  42. </head>
  43. <body>
  44. <div id="products">
  45. <h1 class="ui-widget-header">产品</h1>
  46. <div id="catalog">
  47. <h2><a href="#">T-Shirts</a></h2>
  48. <div>
  49. <ul>
  50. <li>Lolcat Shirt</li>
  51. <li>Cheezeburger Shirt</li>
  52. <li>Buckit Shirt</li>
  53. </ul>
  54. </div>
  55. <h2><a href="#">Bags</a></h2>
  56. <div>
  57. <ul>
  58. <li>Zebra Striped</li>
  59. <li>Black Leather</li>
  60. <li>Alligator Leather</li>
  61. </ul>
  62. </div>
  63. <h2><a href="#">Gadgets</a></h2>
  64. <div>
  65. <ul>
  66. <li>iPhone</li>
  67. <li>iPod</li>
  68. <li>iPad</li>
  69. </ul>
  70. </div>
  71. </div>
  72. </div>
  73. <div id="cart">
  74. <h1 class="ui-widget-header">购物车</h1>
  75. <div class="ui-widget-content">
  76. <ol>
  77. <li class="placeholder">添加产品到这里</li>
  78. </ol>
  79. </div>
  80. </div>
  81. </body>
  82. </html>

我要试一下

简单的照片管理器

您可以通过拖拽照片到回收站或者点击回收站图标来删除照片。

您可以通过拖拽照片到相册或者点击回收利用图标来还原照片。

您可以通过点击缩放图标来查看大图。jQuery UI 对话框(dialog)部件用于模态窗口。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 简单的照片管理器</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. #gallery { float: left; width: 65%; min-height: 12em; }
  12. .gallery.custom-state-active { background: #eee; }
  13. .gallery li { float: left; width: 96px; padding: 0.4em; margin: 0 0.4em 0.4em 0; text-align: center; }
  14. .gallery li h5 { margin: 0 0 0.4em; cursor: move; }
  15. .gallery li a { float: right; }
  16. .gallery li a.ui-icon-zoomin { float: left; }
  17. .gallery li img { width: 100%; cursor: move; }
  18. #trash { float: right; width: 32%; min-height: 18em; padding: 1%; }
  19. #trash h4 { line-height: 16px; margin: 0 0 0.4em; }
  20. #trash h4 .ui-icon { float: left; }
  21. #trash .gallery h5 { display: none; }
  22. </style>
  23. <script>
  24. $(function() {
  25. // 这是相册和回收站
  26. var $gallery = $( "#gallery" ),
  27. $trash = $( "#trash" );
  28. // 让相册的条目可拖拽
  29. $( "li", $gallery ).draggable({
  30. cancel: "a.ui-icon", // 点击一个图标不会启动拖拽
  31. revert: "invalid", // 当未被放置时,条目会还原回它的初始位置
  32. containment: "document",
  33. helper: "clone",
  34. cursor: "move"
  35. });
  36. // 让回收站可放置,接受相册的条目
  37. $trash.droppable({
  38. accept: "#gallery > li",
  39. activeClass: "ui-state-highlight",
  40. drop: function( event, ui ) {
  41. deleteImage( ui.draggable );
  42. }
  43. });
  44. // 让相册可放置,接受回收站的条目
  45. $gallery.droppable({
  46. accept: "#trash li",
  47. activeClass: "custom-state-active",
  48. drop: function( event, ui ) {
  49. recycleImage( ui.draggable );
  50. }
  51. });
  52. // 图像删除功能
  53. var recycle_icon = "<a href='link/to/recycle/script/when/we/have/js/off' title='还原图像' class='ui-icon ui-icon-refresh'>还原图像</a>";
  54. function deleteImage( $item ) {
  55. $item.fadeOut(function() {
  56. var $list = $( "ul", $trash ).length ?
  57. $( "ul", $trash ) :
  58. $( "<ul class='gallery ui-helper-reset'/>" ).appendTo( $trash );
  59. $item.find( "a.ui-icon-trash" ).remove();
  60. $item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
  61. $item
  62. .animate({ width: "48px" })
  63. .find( "img" )
  64. .animate({ height: "36px" });
  65. });
  66. });
  67. }
  68. // 图像还原功能
  69. var trash_icon = "<a href='link/to/trash/script/when/we/have/js/off' title='删除图像' class='ui-icon ui-icon-trash'>删除图像</a>";
  70. function recycleImage( $item ) {
  71. $item.fadeOut(function() {
  72. $item
  73. .find( "a.ui-icon-refresh" )
  74. .remove()
  75. .end()
  76. .css( "width", "96px")
  77. .append( trash_icon )
  78. .find( "img" )
  79. .css( "height", "72px" )
  80. .end()
  81. .appendTo( $gallery )
  82. .fadeIn();
  83. });
  84. }
  85. // 图像预览功能,演示 ui.dialog 作为模态窗口使用
  86. function viewLargerImage( $link ) {
  87. var src = $link.attr( "href" ),
  88. title = $link.siblings( "img" ).attr( "alt" ),
  89. $modal = $( "img[src$='" + src + "']" );
  90. if ( $modal.length ) {
  91. $modal.dialog( "open" );
  92. } else {
  93. var img = $( "<img alt='" + title + "' width='384' height='288' style='display: none; padding: 8px;' />" )
  94. .attr( "src", src ).appendTo( "body" );
  95. setTimeout(function() {
  96. img.dialog({
  97. title: title,
  98. width: 400,
  99. modal: true
  100. });
  101. }, 1 );
  102. }
  103. }
  104. // 通过事件代理解决图标行为
  105. $( "ul.gallery > li" ).click(function( event ) {
  106. var $item = $( this ),
  107. $target = $( event.target );
  108. if ( $target.is( "a.ui-icon-trash" ) ) {
  109. deleteImage( $item );
  110. } else if ( $target.is( "a.ui-icon-zoomin" ) ) {
  111. viewLargerImage( $target );
  112. } else if ( $target.is( "a.ui-icon-refresh" ) ) {
  113. recycleImage( $item );
  114. }
  115. return false;
  116. });
  117. });
  118. </script>
  119. </head>
  120. <body>
  121. <div class="ui-widget ui-helper-clearfix">
  122. <ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">
  123. <li class="ui-widget-content ui-corner-tr">
  124. <h5 class="ui-widget-header">High Tatras</h5>
  125. <img src="/wp-content/uploads/2014/03/high_tatras_min.jpg" alt="High Tatras 的最高峰" width="96" height="72">
  126. <a href="/wp-content/uploads/2014/03/high_tatras.jpg" title="查看大图" class="ui-icon ui-icon-zoomin">查看大图</a>
  127. <a href="link/to/trash/script/when/we/have/js/off" title="删除图像" class="ui-icon ui-icon-trash">删除图像</a>
  128. </li>
  129. <li class="ui-widget-content ui-corner-tr">
  130. <h5 class="ui-widget-header">High Tatras 2</h5>
  131. <img src="/wp-content/uploads/2014/03/high_tatras2_min.jpg" alt="绿山湖畔的小屋" width="96" height="72">
  132. <a href="/wp-content/uploads/2014/03/high_tatras2.jpg" title="查看大图" class="ui-icon ui-icon-zoomin">查看大图</a>
  133. <a href="link/to/trash/script/when/we/have/js/off" title="删除图像" class="ui-icon ui-icon-trash">删除图像</a>
  134. </li>
  135. <li class="ui-widget-content ui-corner-tr">
  136. <h5 class="ui-widget-header">High Tatras 3</h5>
  137. <img src="/wp-content/uploads/2014/03/high_tatras3_min.jpg" alt="计划登高" width="96" height="72">
  138. <a href="/wp-content/uploads/2014/03/high_tatras3.jpg" title="查看大图" class="ui-icon ui-icon-zoomin">查看大图</a>
  139. <a href="link/to/trash/script/when/we/have/js/off" title="删除图像" class="ui-icon ui-icon-trash">删除图像</a>
  140. </li>
  141. <li class="ui-widget-content ui-corner-tr">
  142. <h5 class="ui-widget-header">High Tatras 4</h5>
  143. <img src="/wp-content/uploads/2014/03/high_tatras4_min.jpg" alt="在 Kozi kopka 的顶部" width="96" height="72">
  144. <a href="/wp-content/uploads/2014/03/high_tatras4.jpg" title="查看大图" class="ui-icon ui-icon-zoomin">查看大图</a>
  145. <a href="link/to/trash/script/when/we/have/js/off" title="删除图像" class="ui-icon ui-icon-trash">删除图像</a>
  146. </li>
  147. </ul>
  148. <div id="trash" class="ui-widget-content ui-state-default">
  149. <h4 class="ui-widget-header"><span class="ui-icon ui-icon-trash">回收站</span> 回收站</h4>
  150. </div>
  151. </div>
  152. </body>
  153. </html>

我要试一下

视觉反馈

当悬停在 droppable 上时,或者当 droppable 被激活(即一个可接受的 draggable 被放置在 droppable 上)时,改变 droppable 的外观。使用 hoverClassactiveClass 选项来分别指定 class。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 放置(Droppable) - 视觉反馈</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: 90px; height: 90px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  12. #droppable, #droppable2 { width: 120px; height: 120px; padding: 0.5em; float: left; margin: 10px; }
  13. h3 { clear: left; }
  14. </style>
  15. <script>
  16. $(function() {
  17. $( "#draggable" ).draggable();
  18. $( "#droppable" ).droppable({
  19. hoverClass: "ui-state-hover",
  20. drop: function( event, ui ) {
  21. $( this )
  22. .addClass( "ui-state-highlight" )
  23. .find( "p" )
  24. .html( "Dropped!" );
  25. }
  26. });
  27. $( "#draggable2" ).draggable();
  28. $( "#droppable2" ).droppable({
  29. accept: "#draggable2",
  30. activeClass: "ui-state-default",
  31. drop: function( event, ui ) {
  32. $( this )
  33. .addClass( "ui-state-highlight" )
  34. .find( "p" )
  35. .html( "Dropped!" );
  36. }
  37. });
  38. });
  39. </script>
  40. </head>
  41. <body>
  42. <h3>当悬停在 droppable 上时的反馈:</h3>
  43. <div id="draggable" class="ui-widget-content">
  44. <p>请拖拽我到目标</p>
  45. </div>
  46. <div id="droppable" class="ui-widget-header">
  47. <p>请放置在这里</p>
  48. </div>
  49. <h3>当激活 draggable 时的反馈:</h3>
  50. <div id="draggable2" class="ui-widget-content">
  51. <p>请拖拽我到目标</p>
  52. </div>
  53. <div id="droppable2" class="ui-widget-header">
  54. <p>请放置在这里</p>
  55. </div>
  56. </body>
  57. </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号