天天看點

jquery sortable的拖動方法内容說明和示例詳解(轉載http://www.jb51.net/article/45803.htm)

所有的事件回調函數都有兩個參數: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("");

}