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

拖拽两个贴贴的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);
}
相关推荐
xqlily9 分钟前
MATLAB安装常见问题解决方案
开发语言·matlab
achene_ql14 分钟前
基于QT和FFmpeg实现自己的视频播放器FFMediaPlayer(一)——项目总览
开发语言·qt·ffmpeg
MeyrlNotFound22 分钟前
(二十一)Java集合框架源码深度解析
java·开发语言
TNTLWT25 分钟前
Qt功能区:Ribbon使用
开发语言·qt·ribbon
Ten peaches32 分钟前
Selenium-Java版(环境安装)
java·前端·selenium·自动化
Enti7c34 分钟前
BOM知识点
javascript
Ronin30539 分钟前
【C++】18.二叉搜索树
开发语言·数据结构·c++
Susea&40 分钟前
初始C++:类和对象(中)
c语言·开发语言·c++
心.c44 分钟前
vue3大事件项目
前端·javascript·vue.js
姜 萌@cnblogs1 小时前
【实战】深入浅出 Rust 并发:RwLock 与 Mutex 在 Tauri 项目中的实践
前端·ai·rust·tauri