使表格的標(biāo)題列可左右拉伸jquery插件封裝
更新時間:2014年11月24日 16:23:47 投稿:hebedich
這篇文章主要介紹了使表格的標(biāo)題列可左右拉伸jquery插件封裝,需要的朋友可以參考下
插件名稱命名為:jquery.tableresize.js,代碼如下:
復(fù)制代碼 代碼如下:
/*
Writen by mlcactus, 2014-11-24
這是我封裝的一個jquery插件,能夠使table的各列可以左右拉伸,從而使寬度變小或變大
用法:
單個table:$("#table_id").tableresize();
頁面所有table:$("table").tableresize();
*/
(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 < 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);
頁面代碼為:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head runat="server">
<title></title>
<style type="text/css" >
td{ text-align:center;}
</style>
<script type="text/javascript" src="script/jquery-1.10.2.js"></script>
<script type="text/javascript" src="script/jquery.tableresize.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//使兩張table同時支持左右拉伸
$("table").tableresize();
});
</script>
</head>
<body>
表格1<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" 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>
<br/>表格2<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" 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>
</tbody></table>
</body>
</html>
您可能感興趣的文章:
- jQuery中:header選擇器用法實(shí)例
- Jquery修改頁面標(biāo)題title其它JS失效的解決方法
- 讓新消息在網(wǎng)頁標(biāo)題閃爍提示的jQuery代碼
- 基于jquery插件制作左右按鈕與標(biāo)題文字圖片切換效果
- Jquery 設(shè)置標(biāo)題的自動翻轉(zhuǎn)
- jQuery+Ajax實(shí)現(xiàn)表格數(shù)據(jù)不同列標(biāo)題排序(為表格注入活力)
- Jquery創(chuàng)建層顯示標(biāo)題和內(nèi)容且隨鼠標(biāo)移動而移動
- jQuery 標(biāo)題的自動翻轉(zhuǎn)實(shí)現(xiàn)代碼
- jquery實(shí)現(xiàn)點(diǎn)擊彈出帶標(biāo)題欄的彈出層(從右上角飛入)效果
- jQuery實(shí)現(xiàn)點(diǎn)擊標(biāo)題輸入詳細(xì)信息
- jQuery實(shí)現(xiàn)獲取h1-h6標(biāo)題元素值的方法
相關(guān)文章
JQUERY 瀏覽器判斷實(shí)現(xiàn)函數(shù)
JQUERY 瀏覽器判斷實(shí)現(xiàn)函數(shù),如果只是為了判斷瀏覽器不建議用,如果你已經(jīng)用了jquery才用啊,要不沒必要因?yàn)檫@個小功能,加個那么大的類庫吧。2009-08-08JQueryEasyUI datagrid框架的進(jìn)階使用
本篇文章小編為大家介紹JQueryEasyUI datagrid框架的基本使用,有需要的朋友可以參考一下2013-04-04jQuery+css3實(shí)現(xiàn)轉(zhuǎn)動的正方形效果(附demo源碼下載)
這篇文章主要介紹了jQuery+css3實(shí)現(xiàn)轉(zhuǎn)動的正方形效果,可實(shí)現(xiàn)圖片組集體轉(zhuǎn)動的功能,通過jQuery結(jié)合時間函數(shù)定時操作css3屬性實(shí)現(xiàn)旋轉(zhuǎn)效果,并附帶demo源碼供讀者下載,需要的朋友可以參考下2016-01-01jQuery在線選座位插件seat-charts特效代碼分享
這篇文章主要介紹了jQuery在線選座位插件seat-charts特效,推薦給大家,有需要的小伙伴可以參考下。2015-08-08JQuery基于FormData異步提交數(shù)據(jù)文件
這篇文章主要介紹了JQuery基于FormData異步提交數(shù)據(jù)文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09jQuery實(shí)現(xiàn)的placeholder效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的placeholder效果,可實(shí)現(xiàn)輸入框提示文字的功能,并且針對焦點(diǎn)的情況判定是否顯示,非常簡單實(shí)用,需要的朋友可以參考下2016-08-08jQuery實(shí)現(xiàn)的簡單日歷組件定義與用法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡單日歷組件定義與用法,結(jié)合實(shí)例形式詳細(xì)分析了基于jQuery擴(kuò)展實(shí)現(xiàn)日歷功能的相關(guān)操作技巧,需要的朋友可以參考下2018-12-12