jQuery实现拖动调整表格单元格大小的代码实例


Posted in Javascript onJanuary 13, 2015

jQuery实现的拖动调整表格td单元格的大小:

在实际应用中,可能有这样的需求,那就是需要调整td单元格的大小。
也许是为了便于观察,也许是其他原因,反正这样的需求是有的,下面就分享一段能够实现此功能的代码。

代码实例如下:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<title>三水点靠木</title>

<style type="text/css" >

table {

  border-collapse: collapse;

}

td {

  text-align: center;

}

</style>

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>

<script type="text/javascript">

(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); 

   

$(document).ready(function () {

  $("table").tableresize();

});

</script>

</head>

<body>

<table cellspacing="0" border="1" 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>

</body>

</html>
Javascript 相关文章推荐
jQuery写的日历(包括日历的样式及功能)
Apr 23 Javascript
js过滤HTML标签以及空格的思路及代码
May 24 Javascript
深入了解Node.js中的一些特性
Sep 25 Javascript
node.js中的require使用详解
Dec 15 Javascript
Javascript实现获取及设置光标位置的方法
Jul 21 Javascript
跟我学习javascript的严格模式
Nov 16 Javascript
ionic由于使用了header和subheader导致被遮挡的问题的两种解决方法
Sep 22 Javascript
jQuery EasyUI右键菜单实现关闭标签/选项卡
Oct 10 Javascript
详解springmvc 接收json对象的两种方式
Dec 06 Javascript
Node.js文件编码格式的转换的方法
Apr 27 Javascript
微信小程序之左右布局的实现代码
Dec 13 Javascript
Vue常用API、高级API的相关总结
Feb 02 Vue.js
jQuery trigger()方法用法介绍
Jan 13 #Javascript
修改或扩展jQuery原生方法的代码实例
Jan 13 #Javascript
JQuery.get提交页面不跳转的解决方法
Jan 13 #Javascript
15个jquery常用方法、小技巧分享
Jan 13 #Javascript
js对象基础实例分析
Jan 13 #Javascript
浅谈nodeName,nodeValue,nodeType,typeof 的区别
Jan 13 #Javascript
js使用心得分享
Jan 13 #Javascript
You might like
phpmyadmin出现Cannot start session without errors问题解决方法
2014/08/14 PHP
php获取json数据所有的节点路径
2015/05/17 PHP
php微信开发之关注事件
2018/06/14 PHP
基于jquery ajax 用户无刷新登录方法详解
2012/04/28 Javascript
JS获取当前网页大小以及屏幕分辨率等
2014/09/05 Javascript
DOM事件阶段以及事件捕获与事件冒泡先后执行顺序(图文详解)
2015/08/18 Javascript
jQuery实现可展开合拢的手风琴面板菜单
2015/09/15 Javascript
实例详解Nodejs 保存 payload 发送过来的文件
2016/01/14 NodeJs
TypeScript Type Innference(类型判断)
2016/03/10 Javascript
node.js实现快速截图
2016/08/27 Javascript
Websocket协议详解及简单实例代码
2016/12/12 Javascript
canvas实现流星雨的背景效果
2017/01/13 Javascript
vue+ElementUI实现订单页动态添加产品数据效果实例代码
2017/07/13 Javascript
jQuery常用选择器详解
2017/07/17 jQuery
[01:37]TI4西雅图DOTA2前线报道 VG拿下首胜教练357给出获胜秘诀
2014/07/10 DOTA
[58:57]2018DOTA2亚洲邀请赛3月29日小组赛B组 Effect VS VGJ.T
2018/03/30 DOTA
[01:20:47]DOTA2-DPC中国联赛 正赛 Ehome vs Magma BO3 第一场 1月19日
2021/03/11 DOTA
在Python的Django框架中生成CSV文件的方法
2015/07/22 Python
Python TestCase中的断言方法介绍
2019/05/02 Python
python实现高斯判别分析算法的例子
2019/12/09 Python
python列表切片和嵌套列表取值操作详解
2020/02/27 Python
Python使用进程Process模块管理资源
2020/03/05 Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
2020/04/29 Python
台湾菁英交友:结识黄金单身的台湾人
2018/01/22 全球购物
美国宠物美容和宠物用品购物网站:Cherrybrook
2018/12/07 全球购物
介绍一下Linux文件的记录形式
2012/04/18 面试题
小学生防溺水广播稿
2014/01/12 职场文书
大型主题婚礼活动策划方案
2014/09/15 职场文书
“向国旗敬礼”活动策划方案(4篇)
2014/09/27 职场文书
群众路线调研报告范文
2014/11/03 职场文书
文言文辞职信
2015/02/28 职场文书
销售辞职信范文
2015/03/02 职场文书
大学生自荐信范文
2015/03/05 职场文书
2015年财务人员个人工作总结
2015/07/27 职场文书
给学校的建议书400字
2015/09/14 职场文书
图片批量处理 - 尺寸、格式、水印等
2022/03/07 杂记