• 做了一个jquery插件,使表格的标题列可左右拉伸


     示例下载

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

    /*
    Writen by mlcactus, 2014-11-24
    这是我封装的一个jquery插件,能够使table的各列可以左右拉伸,从而使宽度变小或变大
    用法:$("#table_id").tableresize();
    */
    (function ($) {
        $.fn.tableresize = function (options) {
            var defaults = {
                //当table的宽度到达默认最大值时,是否继续增大以至于出现横向滚动条
                resizeTable: true
            }; 
            var opts = $.extend(defaults, options);
    
    
            var _document = $("body");
            //设定user-select样式,防止内容被选中
            var set_user_select = function (jqueryobj, val) {
                jqueryobj.css("-moz-user-select", val).css("-webkit-user-select", val).css("-ms-user-select", val);
            };
            $(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.outerWidth(), height = _this.outerHeight(), right = left + width, bottom = top + height, pageX = e.pageX, pageY = e.pageY;
                    var leftside = !_firstth.is(_this) && Math.abs(left - pageX) <= 5, rightside = Math.abs(right - pageX) <= 5;
                    if (cobjs._currentLeft || pageY > top && pageY < bottom && (leftside || rightside)) {
                        _document.css("cursor", "e-resize");
                        set_user_select(_table, "none");
                        if (!cobjs._currentLeft) {
                            if (leftside) {
                                cobjs._currentObj = _this.prev();
                            }
                            else {
                                cobjs._currentObj = _this;
                            }
                        }
                    }
                    else {
                        _document.css("cursor", "auto");
                        cobjs._currentObj = null;
                    }
                });
                ths.mouseout(function (e) {
                    if (!cobjs._currentLeft) {
                        cobjs._currentObj = null;
                        _document.css("cursor", "auto");
                        set_user_select(_table, "auto");
                    }
                });
                _document.mousedown(function (e) {
                    if (cobjs._currentObj) {
                        cobjs._currentLeft = e.pageX;
                    }
                    else {
                        cobjs._currentLeft = null;
                    }
                });
                _document.mouseup(function (e) {
                    if (cobjs._currentLeft) {
                        var changeWidth = e.pageX - cobjs._currentLeft;
                        cobjs._currentObj.width(cobjs._currentObj.width() + changeWidth);
                        if (opts.resizeTable) {
                            _table.width(_table.width() + changeWidth);
                        }
                    }
                    cobjs._currentObj = null;
                    cobjs._currentLeft = null;
                    _document.css("cursor", "auto");
                    set_user_select(_table, "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达到默认最大宽度时,将不再继续变宽
                var ops = { resizeTable: false };
                //使两张table同时支持左右拉伸
                $("table").tableresize(ops);
            });
        </script>
    </head>
    <body>
        表格1<br/>
        <table cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
    		<tbody><tr>
    			<td style="200px;">ID</td><td style="200px;">名字</td><td style="200px;">年纪</td><td style="200px;">地址</td><td style="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="200px;">ID</td><td style="200px;">名字</td><td style="200px;">年纪</td><td style="200px;">地址</td><td style="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>
    
  • 相关阅读:
    windows下常用linux对应工具
    常用工具备忘
    spring使用@Value标签读取.properties文件的中文乱码问题的解决
    一致性哈希算法(适用于分库分表、RPC负载均衡)转
    使用事件和消息队列实现分布式事务(转+补充)
    关于架构优化和设计,架构师必须知道的事情(转)
    做了 3 年企业级 SaaS,我收获的 10 点心得(转)
    这五件事,二次SaaS创业的老炮儿都在做(转)
    阿里云端口失效导致tomcat无法对外提供服务
    SaaS公司融资的「22条军规 」(转)
  • 原文地址:https://www.cnblogs.com/mlcactus/p/4118714.html
Copyright © 2020-2023  润新知