首页 > web前端 > js教程 > 正文

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

WBOY
发布: 2016-05-16 16:30:25
原创
1847 人浏览过

插件名称命名为: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)                 if (cobjs._currentLeft || clientY > top && clientY                     _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);  

页面代码为:

复制代码 代码如下:

ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">

    
    
    
    
    


    表格1

    
        
            
        
            
        
            
        
            
        
    
    
ID 名字 年纪 地址 电话
22 Name:44 Age:23 Address:47 Phone:15
28 Name:42 Age:68 Address:30 Phone:50
29 Name:63 Age:48 Address:90 Phone:76

    
表格2

    
        
            
        
            
        
            
        
    
ID 名字 年纪 地址 电话
22 Name:44 Age:23 Address:47 Phone:15
28 Name:42 Age:68 Address:30 Phone:50



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板