拖曳(Draggable)
允許使用滑鼠移動元素。
如需了解更多有關 draggable 互動的細節,請查看 API 文件 可拖曳小工具(Draggable Widget)。
預設功能
在任意的 DOM 元素上啟用 draggable 功能。透過滑鼠點擊並在視區中拖曳來移動 draggable 物件。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 默认功能</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable { width: 150px; height: 150px; padding: 0.5em; } </style> <script> $(function() { $( "#draggable" ).draggable(); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p>请拖动我!</p> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
自動捲動
當draggable 移到視區外時自動捲動文件。設定 scroll
選項為 true 來啟用自動捲動,當滾動觸發時進行微調,滾動速度是透過 scrollSensitivity
和 scrollSpeed
選項設定的。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 自动滚动</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2, #draggable3 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } </style> <script> $(function() { $( "#draggable" ).draggable({ scroll: true }); $( "#draggable2" ).draggable({ scroll: true, scrollSensitivity: 100 }); $( "#draggable3" ).draggable({ scroll: true, scrollSpeed: 100 }); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p>Scroll 设置为 true,默认设置</p> </div> <div id="draggable2" class="ui-widget-content"> <p>scrollSensitivity 设置为 100</p> </div> <div id="draggable3" class="ui-widget-content"> <p>scrollSpeed 设置为 100</p> </div> <div style="height: 5000px; width: 1px;"></div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
約束運動
透過定義draggable 區域的邊界來約束每個draggable 的運動。設定axis
選項來限制draggable 的路徑為x 軸或y 軸,或使用containment
選項來指定一個父級的DOM 元素或是一個jQuery 選擇器,例如'document.' 。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 约束运动</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> .draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } #draggable, #draggable2 { margin-bottom:20px; } #draggable { cursor: n-resize; } #draggable2 { cursor: e-resize; } #containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; } h3 { clear: left; } </style> <script> $(function() { $( "#draggable" ).draggable({ axis: "y" }); $( "#draggable2" ).draggable({ axis: "x" }); $( "#draggable3" ).draggable({ containment: "#containment-wrapper", scroll: false }); $( "#draggable5" ).draggable({ containment: "parent" }); }); </script> </head> <body> <h3>沿着轴约束运动:</h3> <div id="draggable" class="draggable ui-widget-content"> <p>只能垂直拖拽</p> </div> <div id="draggable2" class="draggable ui-widget-content"> <p>只能水平拖拽</p> </div> <h3>或者在另一个 DOM 元素中约束运动:</h3> <div id="containment-wrapper"> <div id="draggable3" class="draggable ui-widget-content"> <p>我被约束在盒子里</p> </div> <div class="draggable ui-widget-content"> <p id="draggable5" class="ui-widget-header">我被约束在父元素内</p> </div> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
遊標樣式
當拖曳物件時定位遊標。預設情況下,遊標是出現在被拖曳物件的中間。使用 cursorAt
選項來指定相對於 draggable 的另一個位置(指定相對於 top、right、bottom、left 的像素值)。透過提供一個帶有有效的 CSS 遊標值的 cursor
選項,來自訂遊標的外觀。有效的 CSS 遊標值包括:default、move、pointer、crosshair,等等。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 光标样式</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2, #draggable3 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } </style> <script> $(function() { $( "#draggable" ).draggable({ cursor: "move", cursorAt: { top: 56, left: 56 } }); $( "#draggable2" ).draggable({ cursor: "crosshair", cursorAt: { top: -5, left: -5 } }); $( "#draggable3" ).draggable({ cursorAt: { bottom: 0 } }); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p>我总是在中间(相对于鼠标)</p> </div> <div id="draggable2" class="ui-widget-content"> <p>我的光标是在 left -5 和 top -5</p> </div> <div id="draggable3" class="ui-widget-content"> <p>我的光标位置只控制了 'bottom' 值</p> </div> </body> </html>
執行實例 »
點擊 "執行實例" 按鈕查看線上實例
延遲開始
透過 delay
選項設定延遲開始拖曳的毫秒數。透過 distance
選項設定遊標被按下且拖曳指定像素後才允許拖曳。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 延迟开始</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2 { width: 120px; height: 120px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } </style> <script> $(function() { $( "#draggable" ).draggable({ distance: 20 }); $( "#draggable2" ).draggable({ delay: 1000 }); $( ".ui-draggable" ).disableSelection(); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p>只有把我拖拽了 20 像素后,拖拽才开始</p> </div> <div id="draggable2" class="ui-widget-content"> <p>不管 distance 是多少,您都必须拖拽并等待 1000ms 后拖拽才开始</p> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
事件
draggable 上的start
、drag
和stop
事件。拖曳開始時觸發 start
事件,拖曳期間觸發 drag
事件,拖曳停止時觸發 stop
事件。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 事件</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable { width: 16em; padding: 0 1em; } #draggable ul li { margin: 1em 0; padding: 0.5em 0; } * html #draggable ul li { height: 1%; } #draggable ul li span.ui-icon { float: left; } #draggable ul li span.count { font-weight: bold; } </style> <script> $(function() { var $start_counter = $( "#event-start" ), $drag_counter = $( "#event-drag" ), $stop_counter = $( "#event-stop" ), counts = [ 0, 0, 0 ]; $( "#draggable" ).draggable({ start: function() { counts[ 0 ]++; updateCounterStatus( $start_counter, counts[ 0 ] ); }, drag: function() { counts[ 1 ]++; updateCounterStatus( $drag_counter, counts[ 1 ] ); }, stop: function() { counts[ 2 ]++; updateCounterStatus( $stop_counter, counts[ 2 ] ); } }); function updateCounterStatus( $event_counter, new_count ) { // 首先更新视觉状态... if ( !$event_counter.hasClass( "ui-state-hover" ) ) { $event_counter.addClass( "ui-state-hover" ) .siblings().removeClass( "ui-state-hover" ); } // ...然后更新数字 $( "span.count", $event_counter ).text( new_count ); } }); </script> </head> <body> <div id="draggable" class="ui-widget ui-widget-content"> <p>请拖拽我,触发一连串的事件。</p> <ul class="ui-helper-reset"> <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> <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> <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> </ul> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
Handles
只有當遊標在draggable 上指定部分時才允許拖曳。使用 handle
選項來指定用於拖曳物件的元素(或元素群組)的 jQuery 選擇器。
handle 選項來指定取消拖曳功能的 jQuery 選擇器。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - Handles</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } #draggable p { cursor: move; } </style> <script> $(function() { $( "#draggable" ).draggable({ handle: "p" }); $( "#draggable2" ).draggable({ cancel: "p.ui-widget-header" }); $( "div, p" ).disableSelection(); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p class="ui-widget-header">您只可以在这个范围内拖拽我</p> </div> <div id="draggable2" class="ui-widget-content"> <p>您可以把我向四周拖拽…</p> <p class="ui-widget-header">…但是您不可以在这个范围内拖拽我</p> </div> </body> </html>
運行實例»點擊"運行實例" 按鈕查看線上實例
revert 選項的draggable 停止拖曳時,返回draggable(或它的助手)到原始位置。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 还原位置</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } </style> <script> $(function() { $( "#draggable" ).draggable({ revert: true }); $( "#draggable2" ).draggable({ revert: true, helper: "clone" }); }); </script> </head> <body> <div id="draggable" class="ui-widget-content"> <p>还原</p> </div> <div id="draggable2" class="ui-widget-content"> <p>还原助手</p> </div> </body> </html>
執行實例 »點擊 "執行實例" 按鈕查看線上實例
對齊到元素或網格
對齊 draggable 到 DOM 元素的內部或外部邊界。使用snap
、snapMode
(inner, outer, both)和snapTolerance
(當呼叫對齊時,draggable 與元素之間的距離,以像素為單位)選項。
或對齊 draggable 到網格。透過 grid
選項設定網格單元的尺寸(以像素為單位的高度或寬度)。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 对齐到元素或网格</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> .draggable { width: 90px; height: 80px; padding: 5px; float: left; margin: 0 10px 10px 0; font-size: .9em; } .ui-widget-header p, .ui-widget-content p { margin: 0; } #snaptarget { height: 140px; } </style> <script> $(function() { $( "#draggable" ).draggable({ snap: true }); $( "#draggable2" ).draggable({ snap: ".ui-widget-header" }); $( "#draggable3" ).draggable({ snap: ".ui-widget-header", snapMode: "outer" }); $( "#draggable4" ).draggable({ grid: [ 20, 20 ] }); $( "#draggable5" ).draggable({ grid: [ 80, 80 ] }); }); </script> </head> <body> <div id="snaptarget" class="ui-widget-header"> <p>我是对齐目标</p> </div> <br style="clear:both"> <div id="draggable" class="draggable ui-widget-content"> <p>默认(snap: true),对齐到所有其他的 draggable 元素</p> </div> <div id="draggable2" class="draggable ui-widget-content"> <p>我只对齐到大盒子</p> </div> <div id="draggable3" class="draggable ui-widget-content"> <p>我只对齐到大盒子的外边缘</p> </div> <div id="draggable4" class="draggable ui-widget-content"> <p>我对齐到一个 20 x 20 网格</p> </div> <div id="draggable5" class="draggable ui-widget-content"> <p>我对齐到一个 80 x 80 网格</p> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
視覺回饋
給使用者回饋,就像以助手方式拖曳物件一樣。 helper
選項接受值'original'(用遊標移動draggable 物件),'clone'(用遊標移動draggable 的副本),或一個傳回DOM 元素的函數(該元素在拖曳期間顯示在遊標附近)。透過 opacity
選項控制助手的透明度。
為了區別哪一個 draggable 正在被拖曳,讓在運動中的 draggable 保持最前。如果正在拖曳,使用zIndex
選項來設定助手的高度z-index,或使用stack
選項來確保當停止拖曳時,最後一個被拖曳的項目總是出現在同組其他項目的上面。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) - 视觉反馈</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> #draggable, #draggable2, #draggable3, #set div { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; } #draggable, #draggable2, #draggable3 { margin-bottom:20px; } #set { clear:both; float:left; width: 368px; height: 120px; } p { clear:both; margin:0; padding:1em 0; } </style> <script> $(function() { $( "#draggable" ).draggable({ helper: "original" }); $( "#draggable2" ).draggable({ opacity: 0.7, helper: "clone" }); $( "#draggable3" ).draggable({ cursor: "move", cursorAt: { top: -12, left: -20 }, helper: function( event ) { return $( "<div class='ui-widget-header'>I'm a custom helper</div>" ); } }); $( "#set div" ).draggable({ stack: "#set div" }); }); </script> </head> <body> <h3 class="docs">助手:</h3> <div id="draggable" class="ui-widget-content"> <p>原始的</p> </div> <div id="draggable2" class="ui-widget-content"> <p>半透明的克隆</p> </div> <div id="draggable3" class="ui-widget-content"> <p>自定义助手(与 cursorAt 结合)</p> </div> <h3 class="docs">堆叠:</h3> <div id="set"> <div class="ui-widget-content"> <p>我们是 draggables..</p> </div> <div class="ui-widget-content"> <p>..它的 z-index 是自动控制的..</p> </div> <div class="ui-widget-content"> <p>..带有 stack 选项。</p> </div> </div> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例
jQuery UI Draggable + Sortable
#Draggable 與Sortable 的無縫互動。
實例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 拖动(Draggable) + 排序(Sortable)</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"> <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="jqueryui/style.css"> <style> ul { list-style-type: none; margin: 0; padding: 0; margin-bottom: 10px; } li { margin: 5px; padding: 5px; width: 150px; } </style> <script> $(function() { $( "#sortable" ).sortable({ revert: true }); $( "#draggable" ).draggable({ connectToSortable: "#sortable", helper: "clone", revert: "invalid" }); $( "ul, li" ).disableSelection(); }); </script> </head> <body> <ul> <li id="draggable" class="ui-state-highlight">请拖拽我</li> </ul> <ul id="sortable"> <li class="ui-state-default">Item 1</li> <li class="ui-state-default">Item 2</li> <li class="ui-state-default">Item 3</li> <li class="ui-state-default">Item 4</li> <li class="ui-state-default">Item 5</li> </ul> </body> </html>
運行實例»
點擊"運行實例" 按鈕查看線上實例