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 相关文章推荐
麻雀虽小五脏俱全 Dojo自定义控件应用
Sep 04 Javascript
收集的10个免费的jQuery相册
Feb 26 Javascript
jQuery EasyUI API 中文文档 - NumberSpinner数值微调器使用介绍
Oct 21 Javascript
jquery自定义类似$.ajax()的方法实现代码
Aug 13 Javascript
JS对字符串编码的几种方式使用指南
May 14 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
Nov 30 Javascript
小发现之浅谈location.search与location.hash的问题
Jun 23 Javascript
JavaScript动态绑定详解
Sep 14 Javascript
微信小程序使用audio组件播放音乐功能示例【附源码下载】
Dec 08 Javascript
vue通过cookie获取用户登录信息的思路详解
Oct 30 Javascript
详解vue父子组件关于模态框状态的绑定方案
Jun 05 Javascript
vue.js实现双击放大预览功能
Jun 23 Javascript
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
创建配置文件 用PHP写出自己的BLOG系统 2
2010/04/12 PHP
PHP添加Xdebug扩展的方法
2014/02/12 PHP
php命令行使用方法和命令行参数说明
2014/04/08 PHP
将二维数组转为一维数组的2种方法
2014/05/26 PHP
php使用Jpgraph绘制简单X-Y坐标图的方法
2015/06/10 PHP
XHProf报告字段含义的解析
2016/05/17 PHP
php删除二维数组中的重复值方法
2018/03/12 PHP
js parseInt(&quot;08&quot;)未指定进位制问题
2010/06/19 Javascript
jquery事件机制扩展插件 jquery鼠标右键事件。
2011/12/26 Javascript
ie支持function.bind()方法实现代码
2012/12/27 Javascript
悬浮数字的实现案例
2014/02/19 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
js实现显示手机号码效果
2017/03/09 Javascript
Angular入口组件(entry component)与声明式组件的区别详解
2018/04/09 Javascript
解决vue axios的封装 请求状态的错误提示问题
2018/09/25 Javascript
angular6开发steps步骤条组件
2019/07/04 Javascript
js实现扫雷源代码
2020/11/27 Javascript
利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)
2021/02/24 Javascript
[06:45]DOTA2卡尔工作室 英雄介绍幻影长矛手篇
2013/07/12 DOTA
[00:59]DOTA2背景故事第二期之四大基本法则
2020/07/07 DOTA
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
python中global用法实例分析
2015/04/30 Python
高效测试用例组织算法pairwise之Python实现方法
2017/07/19 Python
python 读取目录下csv文件并绘制曲线v111的方法
2018/07/06 Python
Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式示例
2019/09/29 Python
pycharm 中mark directory as exclude的用法详解
2020/02/14 Python
Python3爬虫中Selenium的用法详解
2020/07/10 Python
实例讲解CSS3中Transform的perspective属性的用法
2016/04/22 HTML / CSS
国外平面设计第一市场:99designs
2016/10/25 全球购物
台湾森森购物网:U-mall
2017/10/16 全球购物
博士毕业生自我鉴定范文
2014/04/13 职场文书
优秀本科毕业生自荐信
2014/07/04 职场文书
加强作风建设工作总结
2014/10/23 职场文书
巾帼文明岗汇报材料
2014/12/24 职场文书
七一建党节慰问信
2015/02/14 职场文书
培训班开班主持词
2015/07/02 职场文书