首页 前端知识 鼠标拖动改变盒子的大小

鼠标拖动改变盒子的大小

2024-06-16 09:06:20 前端知识 前端哥 954 866 我要收藏

<!DOCTYPE html>
<html lang="en">
  <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>鼠标拖动改变盒子的大小</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      html,
      body {
        width: 100%;
        height: 100%;
        background: #426ab3;
      }
      .box {
        width: 100%;
        height: 400px;
        background: #9b95c9;
        position: absolute;
        bottom: 0;
        left: 0;
      }

      .box-line {
        width: 100%;
        height: 2px;
        background-color: red;
        cursor: n-resize;
      }
    </style>
  </head>
  <body>
    <div class="box" id="box">
      <div class="box-line" id="box-line"></div>
    </div>

    <script>
      let boxLine = document.getElementById('box-line');
      let box = document.getElementById('box');
      let minHeight = 400;
      let maxHeight = 800;
      let height = minHeight;

      boxLine.onmousedown = function (event) {
        const hanldeMouseUp = () => {
          document.removeEventListener('mouseup', hanldeMouseUp);
          document.removeEventListener('mousemove', hanldeMouseMove);
        };

        const hanldeMouseMove = (event) => {
          const documentH = document.documentElement.clientHeight;
          const nowH = documentH - event.clientY;
          if (nowH >= maxHeight) {
            height = maxHeight;
          } else if (nowH <= minHeight) {
            height = minHeight;
          } else {
            height = nowH;
          }

          box.style.height = height + 'px';
        };

        document.addEventListener('mouseup', hanldeMouseUp);
        document.addEventListener('mousemove', hanldeMouseMove);
      };
    </script>
  </body>
</html>

转载请注明出处或者链接地址:https://www.qianduange.cn//article/12329.html
标签
评论
发布的文章

JQuery中的load()、$

2024-05-10 08:05:15

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!