jQuery實現的拖動調整表格td單元格的大小:
在實際應用中,可能有這樣的'需求,那就是需要調整td單元格的大小。
也許是爲了便於觀察,也許是其他原因,反正這樣的需求是有的,下面就分享一段能夠實現此功能的代碼。
代碼實例如下:
複製代碼 代碼如下:
table {
border-collapse: collapse;
}
td {
text-align: center;
}
(function ($){
$eresize = function () {
var _document = $("body");
$(this)(function () {
if (!$eresize) {
$eresize = {};
}
var _table = $(this);
//設定ID
var id = _("id") || "tableresize_" + (om() * 100000)xed(0)ring();
var tr = _("tr")t(), ths = dren(), _firstth = t();
//設定臨時變量存放對象
var cobjs = $eresize[id] = {};
cobjs._currentObj = null, cobjs._currentLeft = null;
emove(function (e) {
var _this = $(this);
var left = _et(),
top = _et(),
width = _h(),
height = _ht(),
right = left + width,
bottom = top + height,
clientX = ntX,
clientY = ntY;
var leftside = !_(_this) && (left - clientX) <= 5,
rightside = (right - clientX) <= 5;
if (cobjs._currentLeft||clientY>top&&clientY
_("cursor", "e-resize");
if (!cobjs._currentLeft) {
if (leftside) {
cobjs._currentObj = _();
}
else {
cobjs._currentObj = _this;
}
}
}
else {
cobjs._currentObj = null;
}
});
eout(function (e) {
if (!cobjs._currentLeft) {
cobjs._currentObj = null;
_("cursor", "auto");
}
});
_edown(function (e) {
if (cobjs._currentObj) {
cobjs._currentLeft = ntX;
}
else {
cobjs._currentLeft = null;
}
});
_eup(function (e) {
if (cobjs._currentLeft) {
cobjs._h(cobjs._h() + (ntX - cobjs._currentLeft));
}
cobjs._currentObj = null;
cobjs._currentLeft = null;
_("cursor", "auto");
});
});
};
})(jQuery);
$(document)y(function () {
$("table")eresize();
});
ID |
名字 |
年紀 |
地址 |
電話 |
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 |