前端开发

 首页 > 前端开发 > jquery教程 > 使表格的标题列可左右拉伸jquery插件封装_jquery

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

分享到:
【字体:
导读:
          这篇文章主要介绍了使表格的标题列可左右拉伸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);  

页面代码为:

复制代码 代码如下:




    
    
    
    
    


    表格1

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

    
表格2

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



分享到:
jQuery实现隔行背景色变色_jquery_自学ph...
隔行变色有很多中方法来实现,之前我都是直接将判断代码写在程序中,今天我们来使用jQuery实现,代码奉上~ jQuery代码: 复制代码 代码如下:        $(function(){             var item = $("tr");             for(var i=0;i
Jquery焦点图实例代码_jquery_自学php网
本文实例讲述了Jquery焦点图实例代码。分享给大家供大家参考。具体如下: 对于很多建站的朋友来讲,焦点图并不陌生,一般的企业站,门户站都会用到焦点图。我们平时在写html代码的时候,很多人为了省时省力,对于焦点图都是在网上下载一些人家写好的代码,直接套上去即可,很多时候我自己也是这样来的。因为一个焦点图有ht...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……