jquery sortable的拖動方法示例詳解(引)


所有的事件回調函數都有兩個參數:event和ui,瀏覽器自有event對象,和經過封裝的ui對象 

ui.helper - 表示sortable元素的JQuery對象,通常是當前元素的克隆對象 
ui.position - 表示相對當前對象,鼠標的坐標值對象{top,left} 
ui.offset - 表示相對於當前頁面,鼠標的坐標值對象{top,left} 
ui.item - 表示當前拖拽的元素 
ui.placeholder - 占位符(如果有定義的話) 
ui.sender - 當前拖拽元素的所屬sortable對象(僅當元素是從另一個sortable對象傳遞過來時有用) 

·參數(參數名 : 參數類型 : 默認值) 
appendTo : String : 'parent' 
Defines where the helper that moves with the mouse is being appended to during the drag (for example, to resolve overlap/zIndex issues). 
初始:$('.selector').sortable({ appendTo: 'body' }); 
獲取:var appendTo = $('.selector').sortable('option', 'appendTo'); 
設置:$('.selector').sortable('option', 'appendTo', 'body'); 

axis : String : false 
如果有設置,則元素僅能橫向或縱向拖動。可選值:'x', 'y' 
初始:$('.selector').sortable({ axis: 'x' }); 
獲取:var axis = $('.selector').sortable('option', 'axis'); 
設置:$('.selector').sortable('option', 'axis', 'x'); 

cancel : Selector : ':input,button' 
阻止排序動作在匹配的元素上發生。 
初始:$('.selector').sortable({ cancel: 'button' }); 
獲取:var cancel = $('.selector').sortable('option', 'cancel'); 
設置:$('.selector').sortable('option', 'cancel', 'button'); 

connectWith : Selector : false 
允許sortable對象連接另一個sortable對象,可將item元素拖拽到另一個中。 
初始:$('.selector').sortable({ connectWith: '.otherlist' }); 
獲取:var connectWith = $('.selector').sortable('option', 'connectWith'); 
設置:$('.selector').sortable('option', 'connectWith', '.otherlist'); 

containment : Element, String, Selector : false 
約束排序動作只能在一個指定的范圍內發生。可選值:DOM對象, 'parent', 'document', 'window', 或jQuery對象 
初始:$('.selector').sortable({ containment: 'parent' }); 
獲取:var containment = $('.selector').sortable('option', 'containment'); 
設置:$('.selector').sortable('option', 'containment', 'parent'); 

cursor : String : 'auto' 
定義在開始排序動作時,如果的樣式。 
初始:$('.selector').sortable({ cursor: 'crosshair' }); 
獲取:var cursor = $('.selector').sortable('option', 'cursor'); 
設置:$('.selector').sortable('option', 'cursor', 'crosshair'); 

cursorAt : Object : false 
當開始移動時,鼠標定位在的某個位置上(最多兩個方向)。可選值:{ top, left, right, bottom }. 
初始:$('.selector').sortable({ cursorAt: 'top' }); 
獲取:var cursorAt = $('.selector').sortable('option', 'cursorAt'); 
設置:$('.selector').sortable('option', 'cursorAt', 'top'); 

delay : Integer : 0 
以毫秒為單位,設置延遲多久才激活排序動作。此參數可防止誤點擊。 
初始:$('.selector').sortable({ delay: 500 }); 
獲取:var delay = $('.selector').sortable('option', 'delay'); 
設置:$('.selector').sortable('option', 'delay', 500); 

distance : Integer : 1 
決定至少要在元素上面拖動多少像素后,才正式觸發排序動作。 
初始:$('.selector').sortable({ distance: 30 }); 
獲取:var distance = $('.selector').sortable('option', 'distance'); 
設置:$('.selector').sortable('option', 'distance', 30); 

dropOnEmpty : Boolean : true 
是否允許拖拽到一個空的sortable對象中。 
初始:$('.selector').sortable({ dropOnEmpty: false }); 
獲取:var dropOnEmpty = $('.selector').sortable('option', 'dropOnEmpty'); 
設置:$('.selector').sortable('option', 'dropOnEmpty', false); 

forceHelperSize : Boolean : false 
If true, forces the helper to have a size. 
初始:$('.selector').sortable({ forceHelperSize: true }); 
獲取:var forceHelperSize = $('.selector').sortable('option', 'forceHelperSize'); 
設置:$('.selector').sortable('option', 'forceHelperSize', true); 

forcePlaceholderSize : Boolean : false 
If true, forces the placeholder to have a size. 
初始:$('.selector').sortable({ forcePlaceholderSize: true }); 
獲取:var forcePlaceholderSize = $('.selector').sortable('option', 'forcePlaceholderSize'); 
設置:$('.selector').sortable('option', 'forcePlaceholderSize', true); 

grid : Array : false 
將排序對象的item元素視為一個格子處理,每次移動都按一個格子大小移動,數組值:[x,y] 
初始:$('.selector').sortable({ grid: [50, 20] }); 
獲取:var grid = $('.selector').sortable('option', 'grid'); 
設置:$('.selector').sortable('option', 'grid', [50, 20]); 

handle : Selector, Element : false 
限制排序的動作只能在item元素中的某個元素開始。 
初始:$('.selector').sortable({ handle: 'h2' }); 
獲取:var handle = $('.selector').sortable('option', 'handle'); 
設置:$('.selector').sortable('option', 'handle', 'h2'); 

helper : String, Function : 'original' 
設置是否在拖拽元素時,顯示一個輔助的元素。可選值:'original', 'clone' 
初始:$('.selector').sortable({ helper: 'clone' }); 
獲取:var helper = $('.selector').sortable('option', 'helper'); 
設置:$('.selector').sortable('option', 'helper', 'clone'); 

items : Selector : '> *' 
指定在排序對象中,哪些元素是可以進行拖拽排序的。 
初始:$('.selector').sortable({ items: 'li' }); 
獲取:var items = $('.selector').sortable('option', 'items'); 
設置:$('.selector').sortable('option', 'items', 'li'); 

opacity : Float : false 
定義當排序時,輔助元素(helper)顯示的透明度。 
初始:$('.selector').sortable({ opacity: 0.6 }); 
獲取:var opacity = $('.selector').sortable('option', 'opacity'); 
設置:$('.selector').sortable('option', 'opacity', 0.6); 

placeholderType: StringDefault: false 
設置當排序動作發生時,空白占位符的CSS樣式。 
初始:$('.selector').sortable({ placeholder: 'ui-state-highlight' }); 
獲取:var placeholder = $('.selector').sortable('option', 'placeholder'); 
設置:$('.selector').sortable('option', 'placeholder', 'ui-state-highlight'); 

revert : Boolean : false 
如果設置成true,則被拖拽的元素在返回新位置時,會有一個動畫效果。 
初始:$('.selector').sortable({ revert: true }); 
獲取:var revert = $('.selector').sortable('option', 'revert'); 
設置:$('.selector').sortable('option', 'revert', true); 

scroll : Boolean : true 
如果設置成true,則元素被拖動到頁面邊緣時,會自動滾動。 
初始:$('.selector').sortable({ scroll: false }); 
獲取:var scroll = $('.selector').sortable('option', 'scroll'); 
設置:$('.selector').sortable('option', 'scroll', false); 

scrollSensitivity : Integer : 20 
設置當元素移動至邊緣多少像素時,便開始滾動頁面。 
初始:$('.selector').sortable({ scrollSensitivity: 40 }); 
獲取:var scrollSensitivity = $('.selector').sortable('option', 'scrollSensitivity'); 
設置:$('.selector').sortable('option', 'scrollSensitivity', 40); 

scrollSpeed : Integer : 20 
設置頁面滾動的速度。 
初始:$('.selector').sortable({ scrollSpeed: 40 }); 
獲取:var scrollSpeed = $('.selector').sortable('option', 'scrollSpeed'); 
設置:$('.selector').sortable('option', 'scrollSpeed', 40); 

tolerance : String : 'intersect' 
設置當拖動元素越過其它元素多少時便對元素進行重新排序。可選值:'intersect', 'pointer' 
intersect:至少重疊50% 
pointer:鼠標指針重疊元素 
初始:$('.selector').sortable({ tolerance: 'pointer' }); 
獲取:var tolerance = $('.selector').sortable('option', 'tolerance'); 
設置:$('.selector').sortable('option', 'tolerance', 'pointer'); 

zIndex : Integer : 1000 
設置在排序動作發生時,元素的z-index值。 
初始:$('.selector').sortable({ zIndex: 5 }); 
獲取:var zIndex = $('.selector').sortable('option', 'zIndex'); 
設置:$('.selector').sortable('option', 'zIndex', 5); 


·事件 

start 
當排序動作開始時觸發此事件。 
定義:$('.selector').sortable({ start: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortstart', function(event, ui) { ... }); 

sort 
當元素發生排序時觸發此事件。 
定義:$('.selector').sortable({ sort: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sort', function(event, ui) { ... }); 

change 
當元素發生排序且坐標已發生改變時觸發此事件。 
定義:$('.selector').sortable({ change: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortchange', function(event, ui) { ... }); 

beforeStop 
當排序動作結束之前觸發此事件。此時占位符元素和輔助元素仍有效。 
定義:$('.selector').sortable({ beforeStop: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortbeforeStop', function(event, ui) { ... }); 

stop 
當排序動作結束時觸發此事件。 
定義:$('.selector').sortable({ stop: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortstop', function(event, ui) { ... }); 

update 
當排序動作結束時且元素坐標已經發生改變時觸發此事件。 
定義:$('.selector').sortable({ update: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortupdate', function(event, ui) { ... }); 

receive 
當一個已連接的sortable對象接收到另一個sortable對象的元素后觸發此事件。 
定義:$('.selector').sortable({ receive: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortreceive', function(event, ui) { ... }); 

over 
當一個元素拖拽移入另一個sortable對象后觸發此事件。 
定義:$('.selector').sortable({ over: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortover', function(event, ui) { ... }); 

out 
當一個元素拖拽移出sortable對象移出並進入另一個sortable對象后觸發此事件。 
定義:$('.selector').sortable({ out: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortout', function(event, ui) { ... }); 

activate 
當一個有使用連接的sortable對象開始排序動作時,所有允許的sortable觸發此事件。 
定義:$('.selector').sortable({ activate: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortactivate', function(event, ui) { ... }); 

deactivate 
當一個有使用連接的sortable對象結束排序動作時,所有允許的sortable觸發此事件。 
定義:$('.selector').sortable({ deactivate: function(event, ui) { ... } }); 
綁定:$('.selector').bind('sortdeactivate', function(event, ui) { ... }); 


·方法 
destory 
從元素中移除拖拽功能。 
用法:.sortable( 'destroy' ) 

disable 
禁用元素的拖拽功能。 
用法:.sortable( 'disable' ) 

enable 
啟用元素的拖拽功能。 
用法:.sortable( 'enable' ) 

option 
獲取或設置元素的參數。 
用法:.sortable( 'option' , optionName , [value] ) 

serialize 
獲取或設置序列化后的每個item元素的id屬性。 
用法:.sortable( 'serialize' , [options] ) 

toArray 
獲取序列化后的每個item元素的id屬性的數組。 
用法:.sortable( 'toArray' ) 

refresh 
手動重新刷新當前sortable對象的item元素的排序。 
用法:.sortable( 'refresh' ) 

refreshPositions 
手動重新刷新當前sortable對象的item元素的坐標,此方法可能會降低性能。 
用法:.sortable( 'refreshPositions' ) 

cancel 
取消當前sortable對象中item元素的排序改變。 
用法:.sortable( 'cancel' ) 


排序后保存有兩種方法,一是cookie,二是數據庫配置文件等。  

下面是數據庫的部分代碼 原作: 
復制代碼代碼如下:

$(function() { 
var show = $(".loader"); 
var orderlist = $(".orderlist"); 
var listleft = $("div[id = 'column_left']"); 
var listcenter = $("div[id = 'column_center']"); 
var listright = $("div[id = 'column_right']"); 
$( ".column" ).sortable({ 
opacity: 0.5,//拖動的透明度 
revert: true, //緩沖效果 
cursor: 'move', //拖動的時候鼠標樣式 
connectWith: ".column", 
//開始用update 結果執行兩次,浪費資源,古改成stop 
//但是stop在元素沒有改變位置的時候也會執行, 
//用update其他js會有問題,^_^ 
stop: function(){ 
var new_order_left = []; //左欄布局 
var new_order_center = [];//中欄布局 
var new_order_right = [];//右欄布局 
listleft.children(".portlet").each(function() { 
new_order_left.push(this.title); 
}); 
listcenter.children(".portlet").each(function() { 
new_order_center.push(this.title); 
}); 
listright.children(".portlet").each(function() { 
new_order_right.push(this.title); 
}); 
var newleftid = new_order_left.join(','); 
var newcenterid = new_order_center.join(','); 
var newrightid = new_order_right.join(','); 
$.ajax({ 
type: "post", 
url: jsonUrl, //服務端處理程序 
data: { leftid: newleftid, centerid: newcenterid, rightid:newrightid}, //id:新的排列對應的ID,order:原排列順序 
// beforeSend: function() { 
// show.html(" 正在更新"); 
// }, 
success: function(msg) { 
//alert(msg); 
show.html(""); 

}); 

}); 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM