创建可调节大小的视图

<div class="container">
  <div class="container__left">Left</div>
  <div class="resizer" data-direction="horizontal"></div>
  <div class="container__right">
    <div class="container__top">Top</div>
    <div class="resizer" data-direction="vertical"></div>
    <div class="container__bottom">Bottom</div>
  </div>
</div>
document.addEventListener("DOMContentLoaded", function () {
  const resizable = function (resizer) {
    const direction = resizer.getAttribute("data-direction") || "horizontal";
    const prevSibling = resizer.previousElementSibling;
    const nextSibling = resizer.nextElementSibling;

    let x = 0;
    let y = 0;
    let prevSiblingHeight = 0;
    let prevSiblingWidth = 0;

    const mouseDownHandler = function (e) {
      x = e.clientX;
      y = e.clientY;
      const rect = prevSibling.getBoundingClientRect();
      prevSiblingHeight = rect.height;
      prevSiblingWidth = rect.width;

      document.addEventListener("mousemove", mouseMoveHandler);
      document.addEventListener("mouseup", mouseUpHandler);
    };

    const mouseMoveHandler = function (e) {
      const dx = e.clientX - x;
      const dy = e.clientY - y;

      switch (direction) {
        case "vertical":
          const h =
            ((prevSiblingHeight + dy) * 100) /
            resizer.parentNode.getBoundingClientRect().height;
          prevSibling.style.height = `${h}%`;
          break;
        case "horizontal":
        default:
          const w =
            ((prevSiblingWidth + dx) * 100) /
            resizer.parentNode.getBoundingClientRect().width;
          prevSibling.style.width = `${w}%`;
          break;
      }

      const cursor = direction === "horizontal" ? "col-resize" : "row-resize";
      resizer.style.cursor = cursor;
      document.body.style.cursor = cursor;

      prevSibling.style.userSelect = "none";
      prevSibling.style.pointerEvents = "none";

      nextSibling.style.userSelect = "none";
      nextSibling.style.pointerEvents = "none";
    };

    const mouseUpHandler = function () {
      resizer.style.removeProperty("cursor");
      document.body.style.removeProperty("cursor");

      prevSibling.style.removeProperty("user-select");
      prevSibling.style.removeProperty("pointer-events");

      nextSibling.style.removeProperty("user-select");
      nextSibling.style.removeProperty("pointer-events");

      // Remove the handlers of `mousemove` and `mouseup`
      document.removeEventListener("mousemove", mouseMoveHandler);
      document.removeEventListener("mouseup", mouseUpHandler);
    };

    resizer.addEventListener("mousedown", mouseDownHandler);
  };

  document.querySelectorAll(".resizer").forEach(function (ele) {
    resizable(ele);
  });
});

html, body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;

  /* Misc */
  border: 1px solid #cbd5e0;
  height: calc(100vh - 2px);
/*   width: 100%; */
}
.resizer[data-direction="horizontal"] {
  background-color: #cbd5e0;
  cursor: ew-resize;
  height: 100%;
  width: 2px;
}
.resizer[data-direction="vertical"] {
  background-color: #cbd5e0;
  cursor: ns-resize;
  height: 2px;
  width: 100%;
}
.container__left {
  /* Initially, the left takes 1/4 width */
  width: 25%;

  /* Misc */
  align-items: center;
  display: flex;
  justify-content: center;
}
.container__right {
  /* Take the remaining width */
  flex: 1;

  /* Misc */
  align-items: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.container__top {
  /* Initial height */
  height: 12rem;

  /* Misc */
  align-items: center;
  display: flex;
  justify-content: center;
}
.container__bottom {
  /* Take the remaining height */
  flex: 1;

  /* Misc */
  align-items: center;
  display: flex;
  justify-content: center;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zero0985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值