jQuery實(shí)現(xiàn)拖動(dòng)調(diào)整表格單元格大小的代碼實(shí)例
jQuery實(shí)現(xiàn)的拖動(dòng)調(diào)整表格td單元格的大小:
在實(shí)際應(yīng)用中,可能有這樣的需求,那就是需要調(diào)整td單元格的大小。
也許是為了便于觀察,也許是其他原因,反正這樣的需求是有的,下面就分享一段能夠?qū)崿F(xiàn)此功能的代碼。
代碼實(shí)例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>腳本之家</title>
<style type="text/css" >
table {
border-collapse: collapse;
}
td {
text-align: center;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
(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è)定臨時(shí)變量存放對(duì)象
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<bottom&&(leftside||rightside)){
_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();
});
</script>
</head>
<body>
<table cellspacing="0" border="1" rules="all">
<tbody>
<tr>
<td style="width:200px;">ID</td>
<td style="width:200px;">名字</td>
<td style="width:200px;">年紀(jì)</td>
<td style="width:200px;">地址</td>
<td style="width:200px;">電話</td>
</tr>
<tr>
<td>22</td>
<td>Name:44</td>
<td>Age:23</td>
<td>Address:47</td>
<td>Phone:15</td>
</tr>
<tr>
<td>28</td>
<td>Name:42</td>
<td>Age:68</td>
<td>Address:30</td>
<td>Phone:50</td>
</tr>
<tr>
<td>29</td>
<td>Name:63</td>
<td>Age:48</td>
<td>Address:90</td>
<td>Phone:76</td>
</tr>
</tbody>
</table>
</body>
</html>
相關(guān)文章
jQuery手機(jī)瀏覽器中拖拽動(dòng)作的艱難性分析
這篇文章主要介紹了jQuery手機(jī)瀏覽器中拖拽動(dòng)作的艱難性分析,實(shí)例分析了常見(jiàn)的jQuery手機(jī)瀏覽器中拖拽動(dòng)作解決方案,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
如何使用CSS3和JQuery easing 插件制作絢麗菜單
這篇文章主要介紹了如何使用CSS3和JQuery easing 插件制作絢麗菜單,這樣做可以讓有菜單的盒子滑出,并且彈出縮略圖。在某些菜單項(xiàng)中我們還包含著有進(jìn)一步鏈接的子菜單。取決于我們鼠標(biāo)在菜單項(xiàng)上的停懸,子菜單將向左或向右滑動(dòng)。,需要的朋友可以參考下2019-06-06
boxy基于jquery的彈出層對(duì)話框插件擴(kuò)展應(yīng)用 彈出層選擇器
當(dāng)大家進(jìn)行復(fù)雜功能設(shè)計(jì)的時(shí)候,在對(duì)多級(jí)聯(lián)選擇進(jìn)行設(shè)計(jì),為了獲得更好的用戶體驗(yàn)和節(jié)省頁(yè)面空間,往往會(huì)使用彈出層的方法。2010-11-11
IE下使用jQuery重置iframe地址時(shí)內(nèi)存泄露問(wèn)題解決辦法
這篇文章主要介紹了IE下使用jQuery重置iframe地址時(shí)內(nèi)存泄露問(wèn)題解決辦法,需要的朋友可以參考下2015-02-02
struts2 jquery 打造無(wú)限層次的樹(shù)
需要:Struts2 , jquery樹(shù)插件 模擬數(shù)據(jù)庫(kù)數(shù)據(jù):每行數(shù)據(jù)包括--自己ID,自己內(nèi)容,父親ID2009-10-10
一個(gè)JQuery寫(xiě)的點(diǎn)擊上下滾動(dòng)的小例子
分享一個(gè)JQuery寫(xiě)的點(diǎn)擊上下滾動(dòng)的小例子,學(xué)習(xí)jquery的朋友可以參考下。2011-08-08
jQuery文字提示與圖片提示效果實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery文字提示與圖片提示效果實(shí)現(xiàn)方法,涉及jQuery針對(duì)鼠標(biāo)事件的響應(yīng)與頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-07-07

