您如何允许用户垂直调整<div>元素的大小?

时间:2019-04-08 00:55:16

标签: html css css3 flexbox slider

我在另一个div中有2个div元素,它们分别显示为块。因此 div1 最终位于 div2 上方。

我要添加某种类型的“栏”,用户可以单击并拖动它,最终将调整 div2 的大小,而 div1 的大小将自动调整。相同数量。

div1 div2 的父项具有样式:display:flex;flex-direction:column; div1 具有flex-grow:1,因此它会自动调整大小

我希望调整大小栏是这样的:

resize Bar

如何添加这样的内容?还有什么办法可以改变CSS的外观?

2 个答案:

答案 0 :(得分:1)

在您的列flexbox 中,您可以在其中一个div上使用resize,并使用设置为其中一个的flex-grow- 下方 就是 slider 并不是非常可定制的:

  • resize: vertical添加到其中一个弹性项目
  • flex: 1添加到另一个弹性项目中(以便弹性项目会随着调整大小的另一个弹性项目的高度变化而自动调整)

body {
  margin: 0;
}

.outer {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.block {
  height: 50%;
}

.block-1 {
  background-color: red;
  resize: vertical; /* resize vertical */
  overflow: auto; /* resize works for overflow other than visible */
}

.block-2 {
  background-color: green;
  flex: 1; /* adjust automatically */
}
<div class="outer">
  <div class="block block-1">
    Block 1
  </div>
  <div class="block block-2">
    Block 2
  </div>
</div>


解决方案

相反,您可以使用{em>注册的mousedown侦听器,更新mousemove高度的block-1侦听器(并重置 mouseup事件)-参见下面的演示:

let block = document.querySelector(".block-1"),
  slider = document.querySelector(".slider");

// on mouse down (drag start)
slider.onmousedown = function dragMouseDown(e) {
  // get position of mouse
  let dragX = e.clientY;
  // register a mouse move listener if mouse is down
  document.onmousemove = function onMouseMove(e) {
    // e.clientY will be the position of the mouse as it has moved a bit now
    // offsetHeight is the height of the block-1
    block.style.height = block.offsetHeight + e.clientY - dragX + "px";
    // update variable - till this pos, mouse movement has been handled
    dragX = e.clientY;
  }
  // remove mouse-move listener on mouse-up (drag is finished now)
  document.onmouseup = () => document.onmousemove = document.onmouseup = null;
}
body {
  margin: 0;
}

.outer {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.block {
  height: 50%;
}

.block-1 {
  background-color: red;
  resize: vertical; /* resize vertical */
  overflow: auto; /* resize works for overflow other than visible */
}

.block-2 {
  background-color: green;
  flex: 1; /* adjust automatically */
  min-height: 0;
  overflow: hidden; /* hide overflow on small width */
}

.slider {
  text-align: center;
  letter-spacing: 10px;
  background-color: #dee2e6;
  cursor: row-resize;
  user-select: none; /* disable selection */
}
<div class="outer">
  <div class="block block-1">
    Block 1
  </div>
  <div class="slider">slider</div>
  <div class="block block-2">
    Block 2
  </div>
</div>

答案 1 :(得分:0)

const resize = document.getElementById('resize');
const handle = document.getElementById('handle');

let yStart;
let height;

handle.addEventListener('mousedown', function($event) {
  yStart = $event.pageY;
  height = resize.offsetHeight;
  handle.addEventListener('mousemove', mousemove);
});

handle.addEventListener('mouseup', function() {
  handle.removeEventListener('mousemove', mousemove);
});

function mousemove($event) {
  resize.style.height = height + ($event.pageY - yStart) + 'px';
}
#resize {
  position: relative;
  border: 1px solid black;
  height: 100px;
  box-sizing: border-box;
}

#handle {
  position: absolute;
  height: 2px;
  width: 100%;
  background: black;
  bottom: 0;
  cursor: row-resize;
}
<div id="resize">
  <div id="handle"></div>
</div>