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 扩展对input的一些操作方法
Oct 30 Javascript
Dom 是什么的详细说明
Oct 25 Javascript
使用js解决由border属性引起的div宽度问题
Nov 26 Javascript
JavaScript中的函数重载深入理解
Aug 04 Javascript
JavaScript里 ==与===区别详解
Aug 16 Javascript
JS实现隐藏同级元素后只显示JS文件内容的方法
Sep 04 Javascript
js模糊查询实例分享
Dec 26 Javascript
简单实现JavaScript弹幕效果
Aug 27 Javascript
AngularJS 购物车全选/取消全选功能的实现方法
Aug 14 Javascript
如何给element添加一个抽屉组件的方法步骤
Jul 14 Javascript
详解将微信小程序接口Promise化并使用async函数
Aug 05 Javascript
vue 实现根据data中的属性值来设置不同的样式
Aug 04 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
phpphp图片采集后按原路径保存图片示例
2014/02/18 PHP
php生成验证码,缩略图及水印图的类分享
2016/04/07 PHP
js正确获取元素样式详解
2009/08/07 Javascript
javascript中的window.location.search方法简介
2013/09/02 Javascript
JavaScript中判断页面关闭、页面刷新的实现代码
2014/08/27 Javascript
JavaScript中exec函数用法实例分析
2015/06/08 Javascript
JavaScript实现添加及删除事件的方法小结
2015/08/04 Javascript
在easyUI开发中,出现jquery.easyui.min.js函数库问题的解决办法
2015/09/11 Javascript
jQuery实现的给图片点赞+1动画效果(附在线演示及demo源码下载)
2015/12/31 Javascript
jstree的简单实例
2016/12/01 Javascript
JS触摸与手势事件详解
2017/05/09 Javascript
AngularJS 中ui-view传参的实例详解
2017/08/25 Javascript
详解.vue文件中监听input输入事件(oninput)
2017/09/19 Javascript
浅谈Koa服务限流方法实践
2017/10/23 Javascript
webpack项目轻松混用css module的方法
2018/06/12 Javascript
vue根据值给予不同class的实例
2018/09/29 Javascript
vue中使用better-scroll实现滑动效果及注意事项
2018/11/15 Javascript
vue实现鼠标经过动画
2019/10/16 Javascript
Vue中的nextTick作用和几个简单的使用场景
2021/01/25 Vue.js
[02:42]DOTA2城市挑战赛收官在即 四强之争风起云涌
2018/06/05 DOTA
Python编程之字符串模板(Template)用法实例分析
2017/07/22 Python
解决pycharm无法调用pip安装的包问题
2018/05/18 Python
Python计算开方、立方、圆周率,精确到小数点后任意位的方法
2018/07/17 Python
Python数据分析matplotlib设置多个子图的间距方法
2018/08/03 Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
2018/11/18 Python
python用requests实现http请求代码实例
2019/10/31 Python
Python实现中值滤波去噪方式
2019/12/18 Python
Python编译为二进制so可执行文件实例
2019/12/23 Python
Pycharm配置lua编译环境过程图解
2020/11/28 Python
python单例模式的应用场景实例讲解
2021/02/24 Python
苹果中国官方网站:Apple中国
2016/07/22 全球购物
成品仓管员岗位职责
2013/12/11 职场文书
奠基仪式策划方案
2014/05/15 职场文书
党支部活动策划方案
2014/08/18 职场文书
用python画城市轮播地图
2021/05/28 Python
MYSQL 表的全面总结
2021/11/11 MySQL