实现侧边栏动态调整宽度效果——简易做法

拖拽两个贴贴的div

HTML结构

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Grid.js 表格</title>
    <style>
      .item {
        display: flex;
        width: 100%;
        height: 300px;
      }
      .item-left {
        flex: 1;
        height: 100%;
        background-color: black;
      }
      .item-right {
        position: relative;
        box-sizing: border-box;
        padding: 0 15px;
        width: 50%;
        height: 100%;
        background-color: aquamarine;
      }
      .item-right-control {
        position: absolute;
        left: 0;
        top: 0;
        font-size: 1;
        cursor: pointer;
        width: 5px;
        background-color: red;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div class="item">
      <div class="item-left" style="color:white;">
        <span>你可以不用假装</span>
      </div>
      <div class="item-right">
        <div class="item-right-control"></div>
        <div class="item-right-body">12321</div>
      </div>
    </div>
    <div class="text"></div>
    <script src="./1.js"> 
    // 代码如下面
    </script>
  </body>
</html>

核心JS代码

javascript 复制代码
let control = document.querySelector(".item-right-control");
let box = document.querySelector(".item-right");
let explain = document.querySelector(".text");
control.addEventListener("mousedown", (e) => {
  document.body.style.userSelect = "none";
  document.addEventListener("mousemove", onMouseMove);
  document.addEventListener("mouseup", onMouseUp);
});
function onMouseMove(e) {
  if (window.innerWidth - e.screenX >= 300 && window.innerWidth - e.screenX <= 1200) {
    box.style.width = box.offsetWidth + (box.offsetLeft - e.screenX) + "px";
  }
}
function onMouseUp() {
  document.body.style.userSelect = "auto";
  document.removeEventListener("mousemove", onMouseMove);
  document.removeEventListener("mouseup", onMouseUp);
}

完整JS代码

javascript 复制代码
let control = document.querySelector(".item-right-control");
let box = document.querySelector(".item-right");
let explain = document.querySelector(".text");
control.addEventListener("mousedown", (e) => {
  document.body.style.userSelect = "none";
  explain.innerHTML = `
    需要改变宽度的盒子距离最左侧屏幕的距离:${box.offsetLeft}<br/>
    盒子的宽度:${box.offsetWidth}<br/>
  `;
  document.addEventListener("mousemove", onMouseMove);
  document.addEventListener("mouseup", onMouseUp);
});
explain.innerHTML = `
    需要改变宽度的盒子距离最左侧屏幕的距离:${box.offsetLeft}<br/>
    盒子的宽度:${box.offsetWidth}<br/>
  `;
function onMouseMove(e) {
  explain.innerHTML = `
    需要改变宽度的盒子距离最左侧屏幕的距离:${box.offsetLeft}<br/>
    盒子的宽度:${box.offsetWidth}<br/>
    盒子改变后的宽度:${box.offsetWidth + box.offsetLeft - e.screenX}<br/>
    鼠标相较于盒子控件的距离:${box.offsetLeft - e.screenX}<br/>
    视口宽度:${window.innerWidth}<br/>
    是否可以拖动:${window.innerWidth - box.offsetLeft >= 300}
  `;
  if (window.innerWidth - e.screenX >= 300 && window.innerWidth - e.screenX <= 1200) {
    box.style.width = box.offsetWidth + (box.offsetLeft - e.screenX) + "px";
  }
}

function onMouseUp() {
  document.body.style.userSelect = "auto";
  document.removeEventListener("mousemove", onMouseMove);
  document.removeEventListener("mouseup", onMouseUp);
}
相关推荐
奇树谦4 分钟前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt
Angel_girl3195 分钟前
vue项目使用svg图标
前端·vue.js
難釋懷10 分钟前
vue 项目中常用的 2 个 Ajax 库
前端·vue.js·ajax
Qian Xiaoo12 分钟前
Ajax入门
前端·ajax·okhttp
VBA633715 分钟前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊24 分钟前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
爱生活的苏苏35 分钟前
vue生成二维码图片+文字说明
前端·vue.js
拉不动的猪37 分钟前
安卓和ios小程序开发中的兼容性问题举例
前端·javascript·面试
炫彩@之星43 分钟前
Chrome书签的导出与导入:步骤图
前端·chrome
码界奇点1 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11