简单实现侧栏拖拉改变宽度(二)

上次实现拖拽改变侧栏的宽度,但是不能隐藏侧栏,这次简单实现一下
代码效果如下
请添加图片描述
代码实现

<!DOCTYPE html>
<html lang="zh-CN">

<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>
    * {
      padding: 0;
      margin: 0;
    }

    .app {
      width: 100%;
      height: 100vh;
      background-color: aliceblue;
      display: flex;
    }

    #slide-bar-box {
      width: 200px;
      height: 100%;
      background-color: #999;
      position: relative;
    }

    #slide-bar {
      color: red;
      font-size: 50px;
      text-align: center;
      padding-right: 6px;
    }

    #drag-btn {
      width: 6px;
      height: 100%;
      background-color: transparent;
      cursor: w-resize;
      position: absolute;
      right: 0;
      top: 0;
    }

    .right {
      height: 100%;
      flex: 1;
      background-color: antiquewhite;
      color: red;
      font-size: 50px;
      text-align: center;
      position: relative;
    }

    #close-btn {
      position: absolute;
      top: 0;
      right: 0;
    }

    #open-btn {
      position: absolute;
      top: 0;
      left: 0;
    }
  </style>
</head>

<body>
  <div class="app">
    <div id="slide-bar-box">
      <div id="slide-bar">侧栏</div>
      <button id="close-btn">关闭</button>
      <div id="drag-btn"></div>
    </div>
    <div class="right"><button id="open-btn">打开</button>右侧</div>
  </div>
</body>
<script>
  let slideBarWidth = 200;//默认为200
  function init() {
    dragSlidBar(20);
    closeSlideBar();
    openSlideBar();
  }
  init();//执行

  /**
     * delay: 防抖时间
     */
  function dragSlidBar(delay) {
    const dragBtn = document.getElementById("drag-btn");
    const slideBarBox = document.getElementById("slide-bar-box");
    //鼠标按下
    dragBtn.onmousedown = function (e) {
      //记录鼠标开始位置
      let startX = e.clientX;
      //防抖初始时间
      let init_time = 0;
      //给得是document绑定事件
      document.onmousemove = function (e) {
        // 防抖
        let now_time = new Date();//现在时间
        if (now_time - init_time > delay) {//满足时间则执行
          init_time = now_time;
          //记录鼠标位置
          let endX = e.clientX;
          //计算鼠标移动距离
          let moveX = endX - startX;
          // 更新鼠标初始位置
          startX = endX;
          // 更新盒子宽度
          slideBarWidth += moveX
          // 边界值处理
          if (slideBarWidth < 200) {
            slideBarWidth = 200;
          }
          if (slideBarWidth > 400) {
            slideBarWidth = 400
          }
          slideBarBox.style.width = slideBarWidth + 'px';

        }
        return false;//阻止默认事件
      };
      //解除事件
      document.onmouseup = function () {
        document.onmousemove = null;
        document.onmouseup = null;
        // 释放鼠标
        dragBtn.releaseCapture && dragBtn.releaseCapture();
      };
      // 捕获鼠标
      dragBtn.setCapture && dragBtn.setCapture();
      return false;//阻止默认事件
    };
  }
    /**
   * 关闭侧栏
  */
  function closeSlideBar() {
    const closeBtn = document.getElementById("close-btn");
    const slideBarBox = document.getElementById("slide-bar-box");
    const slideBar = document.getElementById("slide-bar");
    closeBtn.onclick = function () {
      slideBarBox.style.transition = "width 1s";
      slideBarBox.style.width = '0px';
      slideBar.style.width = (slideBarWidth - 6) + 'px';
    }
  }
  /**
   * 打开侧栏
  */
  function openSlideBar() {
    const openBtn = document.getElementById("open-btn");
    const slideBarBox = document.getElementById("slide-bar-box");
    const slideBar = document.getElementById("slide-bar");
    openBtn.onclick = function () {
      slideBarBox.style.width = slideBarWidth + 'px';
      setTimeout(()=> {
        slideBarBox.style.transition = "width 0s";
      },1000);
      slideBar.style.width = "auto";
    }
  }
</script>

</html>

嗯嗯,就这样咯,有问题请指正;
觉得有用就给个赞吧

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值