jquery 用于调整两个div大小的垂直拖动条

8yparm6h  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(158)

我想要一个垂直的拖动条来调整两个div的大小。我已经创造了一个例子,但我面临着一个问题。
实际:当我调整上面的div大小并向下移动滑块时,父div的区域会增加,因此会给出一个滚动条。
预期:调整大小时,如果滑块向下移动,则应仅显示上部div中包含的数据,当滑块向上移动时,应显示下部div的内容,并且不应增加父div的所有长度。

var handler = document.querySelector('.handler');
var wrapper = handler.closest('.wrapper');
var boxA = wrapper.querySelector('.box1');
var boxB = wrapper.querySelector('.box2');
var isHandlerDragging = false;

document.addEventListener('mousedown', function(e) {
  // If mousedown event is fired from .handler, toggle flag to true
  if (e.target === handler) {
    isHandlerDragging = true;
  }
});

document.addEventListener('mousemove', function(e) {
  // Don't do anything if dragging flag is false
  if (!isHandlerDragging) {
    return false;
  }

  // Get offset
  var containerOffsetTop= wrapper.offsetTop;
  var containerOffsetBottom= wrapper.offsetBottom;


  // Get x-coordinate of pointer relative to container
  var pointerRelativeXpos = e.clientY - containerOffsetTop;
  var pointerRelativeXpos2 = e.clientY - e.offsetTop + e.offsetHeight;
  
 
  var boxAminWidth = 30;

  
  boxA.style.height = (Math.max(boxAminWidth, pointerRelativeXpos - 2)) + 'px';
  boxA.style.flexGrow = 0;
   boxB.style.height = (Math.max(boxAminWidth, pointerRelativeXpos2 - 8)) + 'px';
  boxB.style.flexGrow = 0;
});
document.addEventListener('mouseup', function(e) {
  // Turn off dragging flag when user mouse is up
  isHandlerDragging = false;
});
body {
  margin: 40px;
}

.wrapper {
  background-color: #fff;
  color: #444;
  /* Use flexbox */
  
}

.box1, .box2 {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  margin-top:2%;
  
  /* Use box-sizing so that element's outerwidth will match width property */
  box-sizing: border-box;
  
  /* Allow box to grow and shrink, and ensure they are all equally sized */
 
}

.handler {
  width: 20px;
  height:7px;
  padding: 0;
  cursor: ns-resize;
}

.handler::before {
  content: '';
  display: block;
  width: 100px;
  height: 100%;
  background: red;
  margin: 0 auto;
}
<div class="wrapper">
  <div class="box1">A</div>
  <div class="handler"></div>
  <div class="box2">B</div>
</div>

希望我能清楚地解释我在项目中面临的问题。任何帮助都很感激。

2lpgd968

2lpgd9681#

看起来你在正确的轨道上。您只需要将 Package 器设置为具有flex方向列的flexbox,并为其分配高度。此外,框2需要具有1的flex,以便它可以根据需要增长和收缩。最后,我需要删除JavaScript中将flex grow设置为0的代码。这就是结果。

var handler = document.querySelector('.handler');
    var wrapper = handler.closest('.wrapper');
    var boxA = wrapper.querySelector('.box1');
    var boxB = wrapper.querySelector('.box2');
    var isHandlerDragging = false;

    document.addEventListener('mousedown', function(e) {
        // If mousedown event is fired from .handler, toggle flag to true
        if (e.target === handler) {
            isHandlerDragging = true;
        }
    });

    document.addEventListener('mousemove', function(e) {
        // Don't do anything if dragging flag is false
        if (!isHandlerDragging) {
            return false;
        }

        e.preventDefault();

        // Get offset
        var containerOffsetTop= wrapper.offsetTop;
        var containerOffsetBottom= wrapper.offsetBottom;


        // Get x-coordinate of pointer relative to container
        var pointerRelativeXpos = e.clientY - containerOffsetTop;
        var pointerRelativeXpos2 = e.clientY - e.offsetTop + e.offsetHeight;


        var boxAminWidth = 30;


        boxA.style.height = (Math.max(boxAminWidth, pointerRelativeXpos - 2)) + 'px';
        boxB.style.height = (Math.max(boxAminWidth, pointerRelativeXpos2 - 8)) + 'px';
    });
    document.addEventListener('mouseup', function(e) {
        // Turn off dragging flag when user mouse is up
        isHandlerDragging = false;
    });
body {
            margin: 40px;
        }

        .wrapper {
            background-color: #fff;
            color: #444;
            /* Use flexbox */
            display: flex;
            flex-direction: column;
            height: 200px;
        }

        .box1, .box2 {
            background-color: #444;
            color: #fff;
            border-radius: 5px;
            padding: 20px;
            font-size: 150%;
            margin-top:2%;

            /* Use box-sizing so that element's outerwidth will match width property */
            box-sizing: border-box;

            /* Allow box to grow and shrink, and ensure they are all equally sized */

        }


        .box2 {
            flex: 1;
        }

        .handler {
            width: 20px;
            height:7px;
            padding: 0;
            cursor: ns-resize;
        }

        .handler::before {
            content: '';
            display: block;
            width: 100px;
            height: 100%;
            background: red;
            margin: 0 auto;
        }
<div class="wrapper">
    <div class="box1">A</div>
    <div class="handler"></div>
    <div class="box2">B</div>
</div>

相关问题