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

拖拽两个贴贴的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);
}
相关推荐
OpenTiny社区1 分钟前
2025OpenTiny星光ShowTime!年度贡献者征集启动!
前端·vue.js·低代码
wangan09412 分钟前
不带圆圈的二叉树
java·前端·javascript
狗哥哥13 分钟前
从零到一:打造企业级 Vue 3 高性能表格组件的设计哲学与实践
前端·vue.js·架构
疯狂平头哥15 分钟前
微信小程序真机预览-数字不等宽如何解决
前端
ku_code_ku16 分钟前
python bert_score使用本地模型的方法
开发语言·python·bert
Drift_Dream17 分钟前
前端趣味交互:如何精准判断鼠标从哪个方向进入元素?
前端
hqk19 分钟前
鸿蒙ArkUI:状态管理、应用结构、路由全解析
android·前端·harmonyos
小马哥编程22 分钟前
【软考架构】滑动窗口限流算法的原理是什么?
java·开发语言·架构
云栖梦泽29 分钟前
鸿蒙数据持久化实战:构建本地存储与云同步系统
开发语言·鸿蒙系统
米思特儿林30 分钟前
NuxtImage 配置上传目录配置
前端