首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整整列尺寸

调整整列尺寸
EN

Stack Overflow用户
提问于 2018-04-14 00:35:51
回答 1查看 72关注 0票数 0

我的目标是在纯JavaScript中创建具有可调整大小的列宽度的表。见此链接。当我放大td 1的大小,然后尝试通过拖动td 3的边框使其变小时,除非再次使用td 1,否则不可能使其变小。我猜问题在于它创建了一个具有一定大小的div,在使用td 3的边框拖动时不可能更改这个div。有什么想法吗?我如何从另一个单元格中改变div宽度?

联署材料:

代码语言:javascript
复制
(function () {
    var thElm;
    var startOffset;

    Array.prototype.forEach.call(
      document.querySelectorAll("table td"),
      function (th) {
        th.style.position = 'relative';

        var grip = document.createElement('div');
        grip.innerHTML = " ";
        grip.style.top = 0;
        grip.style.right = 0;
        grip.style.bottom = 0;
        grip.style.width = '5px';
        grip.style.position = 'absolute';
        grip.style.cursor = 'col-resize';
        grip.addEventListener('mousedown', function (e) {
            thElm = th;
            startOffset = th.offsetWidth - e.pageX;
        });

        th.appendChild(grip);
      });

    document.addEventListener('mousemove', function (e) {
      if (thElm) {
        thElm.style.width = startOffset + e.pageX + 'px';
      }
    });

    document.addEventListener('mouseup', function () {
        thElm = undefined;
    });
})();

HTML:

代码语言:javascript
复制
<table>
    <thead>
        <tr>
            <th>th 1</th>
            <th>th 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>td 1</td>
            <td>td 2</td>
        </tr>
        <tr>
            <td>td 3</td>
            <td>td 4</td>
        </tr>
    </tbody>
</table>
EN

回答 1

Stack Overflow用户

发布于 2018-04-14 03:50:56

您的代码正在调整单元格的大小。试着调整标题的大小,而不是单元格。

HTML:

代码语言:javascript
复制
<thead>
    <tr>
        <th data-header-id="col-1">th 1</th>
        <th data-header-id="col-2">th 2</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td data-column-id="col-1">td 1</td>
        <td data-column-id="col-2">td 2</td>
    </tr>
    <tr>
        <td data-column-id="col-1">td 3</td>
        <td data-column-id="col-2">td 4</td>
    </tr>
</tbody>

var thElm;
var startOffset;

联署材料:

代码语言:javascript
复制
Array.prototype.forEach.call(
  document.querySelectorAll("table td"),
  function (th) {
    var columnId = th.attributes["data-column-id"].value;
    th.style.position = 'relative';

    var grip = document.createElement('div');
    grip.innerHTML = "&nbsp;";
    grip.style.top = 0;
    grip.style.right = 0;
    grip.style.bottom = 0;
    grip.style.width = '5px';
    grip.style.position = 'absolute';
    grip.style.cursor = 'col-resize';

    grip.addEventListener('mousedown', function (e) {
        thElm = document.querySelectorAll("[data-header-id='" + columnId + "']")[0];
      console.log(thElm);
        startOffset = thElm.offsetWidth - e.pageX;
    });

    th.appendChild(grip);
  });

document.addEventListener('mousemove', function (e) {
    if (thElm) {
        thElm.style.width = startOffset + e.pageX + 'px';
    }
});

document.addEventListener('mouseup', function () {
    thElm = undefined;
});

这是你的一把小提琴叉,显示了更新的代码:http://jsfiddle.net/54n2rke9/12/

更新:一种替代方法

在这里,不需要更改原始的HTML。

代码语言:javascript
复制
Array.prototype.forEach.call(
  document.querySelectorAll("table td"),
  function (th) {
    th.style.position = 'relative';
    var cellIndex = th.cellIndex;
    ...
    grip.addEventListener('mousedown', function (e) {
          thElm = document.querySelectorAll("th")[cellIndex];
        startOffset = thElm.offsetWidth - e.pageX;
    });

    th.appendChild(grip);
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49826763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档