JQuery表格拖動調(diào)整列寬效果(自己動手寫的)
類似于桌面程序中的表格拖動表頭的效果,當(dāng)鼠標(biāo)停留在表頭邊框線上時,鼠標(biāo)會變成表示左右拖動的形狀,接著拖動鼠標(biāo),會在表格中出現(xiàn)一條隨鼠標(biāo)移動的豎線,最后放開鼠標(biāo),表格列寬會被調(diào)整。最近比較空閑,便自己動手嘗試實(shí)現(xiàn),在此分享下小小的成果。
首先需要如圖所示的鼠標(biāo)圖標(biāo)文件,在自己的硬盤中搜索*.cur,肯定能找到。
為了能在所有需要該效果的頁面使用,并且不需要更改頁面任何HTML,我把所有的代碼整合在 $(document).ready(function() {}); 中,并寫入一個獨(dú)立的JS文件。
用一個1像素寬的DIV來模擬一條豎線,在頁面載入后添加到body元素中
$(document).ready(function() {
$("body").append("<div id=\"line\" style=\"width:1px;height:200px;border-left:1px solid #00000000; position:absolute;display:none\" ></div> ");
});
接下來是鼠標(biāo)移動到表格縱向邊框上鼠標(biāo)變型的問題,起初我考慮在表頭中添加一個很小的塊級元素觸發(fā)mousemove 和mouseout事件,但為了簡單起見,我還是選擇為整個表頭添加該事件。
在TH的mousemove事件中處理鼠標(biāo)變型:
$("th").bind("mousemove", function(event) {
var th = $(this);
//不給第一列和最后一列添加效果
if (th.prevAll().length <= 1 || th.nextAll().length < 1) {
return;
}
var left = th.offset().left;
//距離表頭邊框線左右4像素才觸發(fā)效果
if (event.clientX - left < 4 || (th.width() - (event.clientX - left)) < 4) {
th.css({ 'cursor': '/web/Page/frameset/images/splith.cur' });
//修改為你的鼠標(biāo)圖標(biāo)路徑
}
else {
th.css({ 'cursor': 'default' });
}
});
當(dāng)鼠標(biāo)按下時,顯示豎線,并設(shè)置它的高度,位置CSS屬性,同時記錄當(dāng)前要改變列寬的TH對象,因?yàn)橐粭l邊框線由兩個TH共享,這里總是取前一個TH對象。
$("th").bind("mousedown", function(event) {
var th = $(this);
//與mousemove函數(shù)中同樣的判斷
if (th.prevAll().length < 1 | th.nextAll().length < 1) {
return;
}
var pos = th.offset();
if (event.clientX - pos.left < 4 || (th.width() - (event.clientX - pos.left)) < 4) {
var height = th.parent().parent().height();
var top = pos.top;
$("#line").css({ "height": height, "top": top,"left":event .clientX,"display":"" });
//全局變量,代表當(dāng)前是否處于調(diào)整列寬狀態(tài)
lineMove = true;
//總是取前一個TH對象
if (event.clientX - pos.left < th.width() / 2) {
currTh = th.prev();
}
else {
currTh = th;
}
}
});
接下來是鼠標(biāo)移動時,豎線隨之移動的效果,因?yàn)樾枰?dāng)鼠標(biāo)離開TH元素也要能有該效果,該效果寫在BODY元素的mousemove函數(shù)中
$("body").bind("mousemove", function(event) {
if (lineMove == true) {
$("#line").css({ "left": event.clientX }).show();
}
});
最后是鼠標(biāo)彈起時,最后的調(diào)整列寬效果。這里我給BODY 和TH兩個元素添加了同樣的mouseup代碼。我原先以為我只需要給BODY添加mouseup函數(shù),但不明白為什么鼠標(biāo)在TH中時,事件沒有觸發(fā),我只好給TH元素也添加了代碼。水平有限,下面完全重復(fù)的代碼不知道怎么抽出來。
$("body").bind("mouseup", function(event) {
if (lineMove == true) {
$("#line").hide();
lineMove = false;
var pos = currTh.offset();
var index = currTh.prevAll().length;
currTh.width(event.clientX - pos.left);
currTh.parent().parent().find("tr").each(function() {
$(this).children().eq(index).width(event.clientX - pos.left);
});
}
});
$("th").bind("mouseup", function(event) {
if (lineMove == true) {
$("#line").hide();
lineMove = false;
var pos = currTh.offset();
var index = currTh.prevAll().length;
currTh.width(event.clientX - pos.left);
currTh.parent().parent().find("tr").each(function() {
$(this).children().eq(index).width(event.clientX - pos.left);
});
}
});
好了,只要在需要這個效果的頁面中引入包含以上代碼的JS文件,就可以為頁面中表格添加該效果。
另外以上代碼在火狐中自定義鼠標(biāo)圖標(biāo)的代碼沒出效果,所用的jquery為1.2.6
相關(guān)文章
jquery插件tytabs.jquery.min.js實(shí)現(xiàn)漸變TAB選項(xiàng)卡效果
這篇文章主要介紹了jquery插件tytabs.jquery.min.js實(shí)現(xiàn)漸變TAB選項(xiàng)卡效果,實(shí)例分析了tytabs.jquery.min.js插件實(shí)現(xiàn)tab選項(xiàng)卡切換效果的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jQuery實(shí)現(xiàn)注冊會員時密碼強(qiáng)度提示信息功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)注冊會員時密碼強(qiáng)度提示信息功能,涉及jQuery事件響應(yīng)及字符串的遍歷、運(yùn)算與判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
基于jquery1.4.2的仿flash超炫焦點(diǎn)圖播放效果
有了jquery一切變的如此簡單!讓js做的動畫更有動感。2010-04-04
用JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法
本篇文章主要是對JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02

