欢迎来到代码驿站!

jquery

当前位置:首页 > 网页前端 > jquery

使表格的标题列可左右拉伸jquery插件封装

时间:2022-01-29 09:56:55|栏目:jquery|点击:

插件名称命名为:jquery.tableresize.js,代码如下:

复制代码 代码如下:

/*
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);
            //设定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();
            //设定临时变量存放对象
            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);  

页面代码为:

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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;">年纪</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;">年纪</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>

上一篇:浅谈window.onbeforeunload() 事件调用ajax

栏    目:jquery

下一篇:基于jQuery的时间戳与日期间的转化

本文标题:使表格的标题列可左右拉伸jquery插件封装

本文地址:http://www.codeinn.net/misctech/191725.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有