基于jquery-resizable創(chuàng)建可調(diào)整大小的表(table)格
本文介紹如何使用jquery-resizable調(diào)整表格(table)列寬。
效果圖
示例介紹
鼠標(biāo)拖動單元格邊框,可調(diào)整列寬。
創(chuàng)建可調(diào)整大小的列需要付出一些額外的努力,因為表沒有一種簡單的方法來創(chuàng)建可以移動的拖動句柄。
為了調(diào)整列的大小,我們可以在列的右側(cè)添加一個元素來提供句柄,這樣我們就有了一個用于拖動的視覺指示器。要調(diào)整大小的表格單元格為 position:relative,注入的元素為 position:absolute 并推到單元格右側(cè)以提供拖動手柄。拖動然后簡單地調(diào)整單元格的大小。
在實踐中,這意味著你需要一些CSS(或jquery樣式)來強制注入樣式和邏輯來注入元素。
HTML
<table> <thead> <th> col 1 </th> <th> col 2 </th> <th> col 3 </th> <th> col 4 </th> </thead> <tbody> <tr> <td> Column 1 </td> <td> Column 2 </td> <td> Column 3 </td> <td> Column 4 </td> </tr> <tr> <td> Column 1 </td> <td> Column 2 </td> <td> Column 3 </td> <td> Column 4 </td> </tr> </tbody> </table>
html的table標(biāo)簽為常規(guī)結(jié)構(gòu),不需要添加額外的ID屬性或class屬性。
jQuery
jQuery編程需要先引用jQuery庫文件,以及jquery-resizable.js插件。
<script src='jquery-3.2.1.min.js'></script> <script src='jquery-resizable.js'></script> <script> $("td:first-child,td:nth-child(2),td:nth-child(3),th:first-child,th:nth-child(2),th:nth-child(3)").css({position: "relative" }).prepend("<div class='resizer'></div>").resizable({ resizeHeight: false, handleSelector: "", onDragStart: function (e, $el, opt) { if (!$(e.target).hasClass("resizer")) return false; return true; } }); </script>
上面的jQuery實現(xiàn)代碼,加上注釋文字,你可能更容易理解:
//$("td,th") $("td:first-child,td:nth-child(2),td:nth-child(3),th:first-child,th:nth-child(2),th:nth-child(3)"). css({ /* 需要包含 resizer */ position: "relative" }) /* 檢查 .resizer CSS */. prepend("<div class='resizer'></div>"). resizable({ resizeHeight: false, // 通過 .resizer 元素,使用列作為句柄和篩選器 handleSelector: "", onDragStart: function (e, $el, opt) { // 只拖拽 resizer if (!$(e.target).hasClass("resizer")) return false; return true; } });
總結(jié)
本文介紹了使用jquery-resizable拖動單元格邊框調(diào)整表格(table)列寬的方法,需要該效果的朋友可以直接下載源碼使用。
相關(guān)文章
利用JQuery實現(xiàn)datatables插件的增加和刪除行功能
這篇文章給大家介紹了jquery實現(xiàn)datatables插件的增加和刪除行的功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01使用BootStrap和Metroui設(shè)計的metro風(fēng)格微網(wǎng)站或手機app界面
今天使用bootstrap和metroui設(shè)計了一個metro風(fēng)格的移動app或者微信微網(wǎng)站的界面,非常不錯具有參考借鑒價值,感興趣的朋友可以參考下2016-10-10Javascript中的異步編程規(guī)范Promises/A詳細(xì)介紹
這篇文章主要介紹了Javascript中的異步編程規(guī)范Promises/A詳細(xì)介紹,同時介紹了jQuery 中的 Deferred 和 Promises,需要的朋友可以參考下2014-06-06基于jquery實現(xiàn)的鼠標(biāo)懸停提示案例
本文主要介紹了基于jquery實現(xiàn)的鼠標(biāo)懸停提示的詳細(xì)案例。代碼全面,功能實用,需要的朋友可以參考借鑒2016-12-12