DIV CSS 佈局教程網

用jQuery實現拖動調整表格單元格大小的完整示例
編輯:JQuery特效代碼     

本文分享一段用jQuery實現「拖動調整表格 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="https://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>

這段代碼封裝了一個 tableresize 插件,通過監聽鼠標在單元格邊緣的移動、按下與放開,判斷拖拽方向並動態調整對應列的寬度,从而实现直觀的表格列寬拖拽調整。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved