- 相關(guān)推薦
jQuery實(shí)現(xiàn)的拖動調(diào)整表格單元格的大小代碼實(shí)例
jQuery實(shí)現(xiàn)的拖動調(diào)整表格td單元格的大小:
在實(shí)際應(yīng)用中,可能有這樣的需求,那就是需要調(diào)整td單元格的大小。
也許是為了便于觀察,也許是其他原因,反正這樣的需求是有的,下面就分享一段能夠?qū)崿F(xiàn)此功能的代碼。
代碼實(shí)例如下:
復(fù)制代碼 代碼如下:
table {
border-collapse: collapse;
}
td {
text-align: center;
}
(function ($){
$.fn.tableresize = function () {
var _document = $("body");
$(this).each(function () {
if (!$.tableresize) {
$.tableresize = {};
}
var _table = $(this);
//設(shè)定ID
var id = _table.attr("id") || "tableresize_" + (Math.random() * 100000).toFixed(0).toString();
var tr = _table.find("tr").first(), ths = tr.children(), _firstth = ths.first();
//設(shè)定臨時變量存放對象
var cobjs = $.tableresize[id] = {};
cobjs._currentObj = null, cobjs._currentLeft = null;
ths.mousemove(function (e) {
var _this = $(this);
var left = _this.offset().left,
top = _this.offset().top,
width = _this.width(),
height = _this.height(),
right = left + width,
bottom = top + height,
clientX = e.clientX,
clientY = e.clientY;
var leftside = !_firstth.is(_this) && Math.abs(left - clientX) <= 5,
rightside = Math.abs(right - clientX) <= 5;
if (cobjs._currentLeft||clientY>top&&clientY
_document.css("cursor", "e-resize");
if (!cobjs._currentLeft) {
if (leftside) {
cobjs._currentObj = _this.prev();
}
else {
cobjs._currentObj = _this;
}
}
}
else {
cobjs._currentObj = null;
}
});
ths.mouseout(function (e) {
if (!cobjs._currentLeft) {
cobjs._currentObj = null;
_document.css("cursor", "auto");
}
});
_document.mousedown(function (e) {
if (cobjs._currentObj) {
cobjs._currentLeft = e.clientX;
}
else {
cobjs._currentLeft = null;
}
});
_document.mouseup(function (e) {
if (cobjs._currentLeft) {
cobjs._currentObj.width(cobjs._currentObj.width() + (e.clientX - cobjs._currentLeft));
}
cobjs._currentObj = null;
cobjs._currentLeft = null;
_document.css("cursor", "auto");
});
});
};
})(jQuery);
$(document).ready(function () {
$("table").tableresize();
});
ID |
名字 |
年紀(jì) |
地址 |
電話 |
22 |
Name:44 |
Age:23 |
Address:47 |
Phone:15 |
28 |
Name:42 |
Age:68 |
Address:30 |
Phone:50 |
29 |
Name:63 |
Age:48 |
Address:90 |
Phone:76 |
【jQuery實(shí)現(xiàn)的拖動調(diào)整表格單元格的大小代碼實(shí)例】相關(guān)文章:
基于jQuery的固定表格頭部的代碼08-30
關(guān)jQuery彈出窗口簡單實(shí)現(xiàn)代碼-javascript編程06-07
jquery提交按鈕的代碼07-28
關(guān)于jQuery實(shí)現(xiàn)高亮顯示的方法介紹08-20
JavaScript實(shí)現(xiàn)網(wǎng)頁刷新代碼段08-07
excel怎樣隱藏表格單元格08-05
防盜鏈接ASP函數(shù)實(shí)現(xiàn)代碼01-23