table可更改th大小的jQuery外掛程式

來源:互聯網
上載者:User

標籤:

(function ($) {    $.fn.resizetable = function () {        var tableObj = $(this);        var inResizeRange = false;          //滑鼠移到可調整範圍內為true        var inResizeRangeLClicked = false;  //滑鼠在可調整範圍內右鍵按下為true        var inResizing = false;             //開始調整大小時為true(滑鼠在可調整範圍內,並按下右鍵)        var resizeDiv;      //用來顯示當前位置的div        var nowThObj;       //當前正在調整的th        var dragDirection;  //0 left, 1 right        //如果可調整大小,需要將talbe寬度設定為固定數字,這樣在調整大小後,整個表格的大小也需要調整        var ths = tableObj.find("th");        for (var i = 0; i < ths.length; i++) {            $(ths[i]).width($(ths[i]).width());        }        tableObj.css("width", tableObj.width());        tableObj.find("th").mousemove(function (e) {            //滑鼠在TH移動時,如果在邊界範圍則將滑鼠改成可移動表徵圖(邊界範圍是在邊界的左右2px內)            var pos = $(this).position();            var width = $(this).width();            if (!inResizeRange) {                if (pos.left + width - 2 <= e.clientX) {                    $(this).css("cursor", "col-resize");                    inResizeRange = true;                    nowThObj = this;                    dragDirection = 1;                }                if (e.clientX - 2 <= pos.left + 2) {                    if (this.cellIndex == 0) return;                    $(this).css("cursor", "col-resize");                    inResizeRange = true;                    nowThObj = this;                    dragDirection = 0;                }            }            if (!inResizing) {                if (!(pos.left + width - 2 <= e.clientX || e.clientX - 2 <= pos.left + 2)) {                    $(this).css("cursor", "");                    inResizeRange = false;                }            }        }).mousedown(function (e) {            //在可遷移範圍內按下滑鼠右鍵,建立可移動的div            if (e.button <= 1 && inResizeRange) {                inResizeRangeLClicked = true;                var pos = $(this).position();                createDragDiv(e.clientX, pos.top);            }        });        $("body").mousemove(function (e) {            //移動滑鼠,顯示當前位置            if (inResizeRange && inResizeRangeLClicked) {                $(this).css("cursor", "col-resize");                resizeDiv.css("left", e.clientX);            }        }).mouseup(function (e) {            //滑鼠右鍵彈起,調整TH寬度,並釋放資源            if (e.button <= 1 && inResizeRange) {                adjustWidth(e);                inResizeRangeLClicked = false;                destroy();            }        });        //建立顯示調整位置的DIV        function createDragDiv(posLeft, posTop) {            resizeDiv = $("<div style=‘position:absolute; width:2px; height:50px; background-color:gray;‘></div>");            resizeDiv.appendTo("body").css({ "left": posLeft, "top": posTop });            inResizing = true;        }        //重設各標識變數,釋放div        function destroy() {            inResizeRange = false;            inResizeRangeLClicked = false;            $("body").css("cursor", "");            resizeDiv.remove();            inResizing = false;        }        //調整TH大小        function adjustWidth(e) {            var resizeWidth = 0;            if (dragDirection == 0) {                //left                resizeWidth = e.clientX - $(nowThObj).position().left;                $(nowThObj).prev("th").width($(nowThObj).prev("th").width() + resizeWidth);            } else {                //right                resizeWidth = e.clientX - ($(nowThObj).position().left + $(nowThObj).width());                $(nowThObj).width($(nowThObj).width() + resizeWidth);            }            tableObj.width(tableObj.width() + resizeWidth);        }    };})(jQuery)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <script src="jquery-1.9.1.min.js"></script>    <script src="jquery.resizetable.js"></script>    <script type="text/javascript">        $(function () {            $("#tableTest").resizetable();        });    </script>    <style type="text/css">        table th,td {            border: 1px solid black;        }        body {            -moz-user-select: none;            -webkit-user-select: none;            user-select: none;        }    </style></head>    <body>        <table id="tableTest" cellpadding="0" cellspacing="0" style="border-collapse:collapse; width:100%;">            <tr>                <th>列1</th>                <th>列2</th>                <th>列3</th>            </tr>            <tr>                <td>cell 1</td>                <td>cell 2</td>                <td>cell 3</td>            </tr>        </table>    </body></html>

 

table可更改th大小的jQuery外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.