原生js实现拖拽改变宽度

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        display: flex;
        height: 300px;
      }

      .list-page {
        width: 30%;
        background-color: #ccc;
      }

      .list-detail {
        flex: 1;
        background-color: #eee;
      }

      .drag-line {
        width: 3px;
        background-color: #999;
        cursor: ew-resize;
        z-index: 1;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="list-page">
        <h2>列表区域</h2>
      </div>

      <div class="drag-line" id="drag-line"></div>

      <div class="list-detail">
        <h2>详情区域</h2>
      </div>
    </div>

    <script>
      window.addEventListener('DOMContentLoaded', () => {
        const dragLine = document.getElementById('drag-line');
        const container = document.querySelector('.container');
        const listPage = document.querySelector('.list-page');

        let startX = 0;
        let dragWidth = 0;

        dragLine.addEventListener('mousedown', startDrag);
        dragLine.addEventListener('touchstart', startDrag);

        function startDrag(event) {
          event.preventDefault();
          startX = event.clientX || event.touches[0].clientX;
          dragWidth = listPage.offsetWidth;

          document.documentElement.addEventListener('mousemove', doDrag);
          document.documentElement.addEventListener('touchmove', doDrag);
          document.documentElement.addEventListener('mouseup', stopDrag);
          document.documentElement.addEventListener('touchend', stopDrag);
        }

        function doDrag(event) {
          const clientX = event.clientX || event.touches[0].clientX;
          const offsetX = clientX - startX;
          const containerWidth = container.offsetWidth;

          let newDragWidth = dragWidth + offsetX;

          // 添加最小宽度和最大宽度限制
          if (newDragWidth < 100) {
            newDragWidth = 100;
          } else if (newDragWidth > 800) {
            newDragWidth = 800;
          }

          const newListPageWidth = (newDragWidth / containerWidth) * 100;
          const newListDetailWidth = 100 - newListPageWidth;

          listPage.style.width = newListPageWidth + '%';
          listPage.style.flex = `0 0 ${newListPageWidth}%`;
          listPage.style.maxWidth = newListPageWidth + '%';

          listDetail.style.width = newListDetailWidth + '%';
          listDetail.style.flex = `0 0 ${newListDetailWidth}%`;
          listDetail.style.maxWidth = newListDetailWidth + '%';
        }

        function stopDrag() {
          document.documentElement.removeEventListener('mousemove', doDrag);
          document.documentElement.removeEventListener('touchmove', doDrag);
          document.documentElement.removeEventListener('mouseup', stopDrag);
          document.documentElement.removeEventListener('touchend', stopDrag);
        }
      });
    </script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 HTML 表格宽度拖拽,可以使用原生 JavaScript实现。以下是一个简单的实现方式: HTML 代码: ``` <table id="myTable"> <tr> <th>列1</th> <th>列2</th> <th>列3</th> </tr> <tr> <td>内容1</td> <td>内容2</td> <td>内容3</td> </tr> <tr> <td>内容4</td> <td>内容5</td> <td>内容6</td> </tr> </table> ``` JavaScript 代码: ``` var table = document.getElementById("myTable"); var isResizing = false; var lastDownX = 0; // 设置每列的初始宽度 var columnWidths = [100, 100, 100]; for (var i = 0; i < table.rows[0].cells.length; i++) { table.rows[0].cells[i].style.width = columnWidths[i] + "px"; } // 鼠标按下时记录位置 table.addEventListener("mousedown", function (e) { isResizing = true; lastDownX = e.clientX; }); // 鼠标移动时改变宽度 table.addEventListener("mousemove", function (e) { if (!isResizing) { return; } var cell = e.target.parentElement; var cellIndex = Array.from(cell.parentElement.children).indexOf(cell); var widthDiff = e.clientX - lastDownX; // 改变当前列的宽度 var newWidth = columnWidths[cellIndex] + widthDiff; if (newWidth > 0) { cell.style.width = newWidth + "px"; columnWidths[cellIndex] = newWidth; lastDownX = e.clientX; } }); // 鼠标抬起时停止改变宽度 table.addEventListener("mouseup", function (e) { isResizing = false; }); ``` 以上代码会在表格中添加一个鼠标事件监听器,当用户按下鼠标并移动时,会根据鼠标的位置改变被选中列的宽度。需要注意的是,当用户拖动宽度时,应该记录每列的初始宽度(在这里使用了 `columnWidths` 数组来记录),并在移动过程中修改当前列的宽度,而不是仅仅修改当前列的宽度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值