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

拖拽两个贴贴的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);
}
相关推荐
RAY_CHEN.1 天前
vue递归组件-笔记
前端·javascript·vue.js
WenGyyyL1 天前
GMNER多模态实体识别任务——ReAct结合
前端·react.js·前端框架
周杰伦_Jay1 天前
【主流开发语言深度对比】Python/Go/Java/JS/Rust/C++评测
开发语言·python·golang
晴殇i1 天前
千万级点赞系统架构演进:从单机数据库到分布式集群的完整解决方案
前端·后端·面试
ldmd2841 天前
Go语言实战:入门篇-5:函数、服务接口和Swagger UI
开发语言·后端·golang
CDwenhuohuo1 天前
WebSocket 前端node启用ws调试
前端·websocket·网络协议
光子物联单片机1 天前
C语言基础开发入门系列(八)C语言指针的理解与实战
c语言·开发语言·stm32·单片机·mcu
是苏浙1 天前
零基础入门C语言之文件操作
c语言·开发语言
盈电智控1 天前
体力劳动反而更难被AI取代?物联网科技如何守护最后的劳动阵地
开发语言·人工智能·python
隔壁阿布都1 天前
Spring Boot中的Optional如何使用
开发语言·spring boot·python