首页 > 语言 > JavaScript > 正文

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

2024-05-06 14:47:34
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选