从两端调整 div 的大小

6
我正在制作一个div调整大小的功能,由于需要根据其基础进行自定义,因此无法使用任何插件。我已经成功实现了从右侧调整div大小的任务。在此过程中,我正在操纵拖动并相应地计算单位。
如果我限制拖动到右侧,这个脚本可以正常工作。但是现在我的任务是在两端调整大小。我知道需要应用某些技术。
我正在尝试应用的一种技术是将div分为两半,并注意从该中心点的距离,例如,如果中心点为200px,鼠标在10px处,则我们可以从右侧开始减小div的大小,反之亦然。

var handle, measurement, isResizing;
var pageWidth = $(window).width();
var maxUnit = 300;
var minUnit = 50;
var maxLimit;
var adjustment = 0;
var container;

function calculateUnit(maxUnit, maxLimit, currentWidth) {
  var offset = maxLimit - currentWidth;
  return Math.ceil(maxUnit - offset);
}

function adjustContainer(innerContainerWidth, widthDiff, heightDiff) {
  handle.css({
    'width': (innerContainerWidth - widthDiff) + 'px',
    'left': (widthDiff / 2) + 'px',
    'top': (heightDiff / 2) + 'px'
  });
}

function InitSizeCalculator() {
  container = $("#topDrag");
  console.log('height c', container.height());
  //console.log('width c', container.width());
  handle = $('#drag'), measurement = document.getElementById('measurement'), isResizing = false;
  var heightDiff = container.height() - 170;
  var widthDiff = container.width() - handle.width();
  console.log('height c', heightDiff);
  //maxLimit = (pageWidth <= 720) ? (pageWidth - 20) : (pageWidth - (pageWidth / 3)) - 60;
  maxLimit = container.width();
  adjustContainer(handle.width(), widthDiff, heightDiff);
  //handle.css('width', maxLimit);

  measurement.innerHTML = maxUnit + ' m';
}
InitSizeCalculator(); //initialize the variable first 
handle.on('mousedown touchstart', function(e) {
  isResizing = true;
  lastDownX = e.clientX;
});

$(document).on('mousemove touchmove', function(e) {
    var currentWidth = e.clientX - adjustment;
    console.log(e.clientX);
    // we don't want to do anything if we aren't resizing.
    var unit = calculateUnit(maxUnit, maxLimit, currentWidth);
    if (!isResizing || unit < minUnit || e.clientX > maxLimit)
      return;
    handle.css('width', currentWidth);
    measurement.innerHTML = unit + ' cm';
  })
  .on('mouseup touchend', function(e) {
    // stop resizing
    isResizing = false;
  });
//start
.imgContainer-p {
  position: relative !important;
  border-right: black 1px dashed;
  border-left: black 1px dashed;
  cursor: w-resize;
  height: 220px
}
#drag {
  position: absolute;
  /*right: 500px;*/
  top: 0;
  bottom: 0;
  /*width: 500px;*/
}
.imgWinder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  /*height: 200px;*/
  height: 90%;
}
.imgPaper {
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  /*height: 200px;*/
  height: 90%;
}
.measurment-p {
  width: 100%;
  height: 20px;
  border-bottom: 1px dashed black;
  border-left: 1px dashed black;
  border-right: 0px dashed black;
  padding-top: 10px;
  text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-9 col-sm-12" id="topDrag">
  <div class="imgContainer-p" id="drag">
    <img id="imgWinder" class="imgWinder" draggable="false" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Cylinder_geometry_rotated.svg/2000px-Cylinder_geometry_rotated.svg.png" />


    <div style="width: 100%; height: 20px; border-bottom: 1px dashed black; border-left: 1px dashed black; border-right: 0px dashed black; padding-top: 10px; text-align: center">
      <span style="background-color: #FFFFFF; padding: 0 10px;">
                                    <span class="label label-default">Size</span>
      <span class="label label-warning" id="measurement">01</span>

      <!--Padding is optional-->
      </span>
    </div>

  </div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

1个回答

2
  1. 当您从右侧拖动元素以展开它时,实际上是通过将拖动操作中覆盖的像素数添加到其宽度中来更新元素的右侧位置。
  2. 这就是元素在浏览器中的行为方式 - 从左到右,因此类似于使用CSS增加宽度。但是,当您拖动它的相反面,即左侧,浏览器不知道如何处理。
  3. 现在,由于您正在将元素调整大小到左侧,因此您在那里有空间,并且元素的左边缘移动到该空间的新位置,但是您无法向左侧添加宽度。因此,您必须通过保持右侧边缘处于当前位置来产生它被添加到左侧的幻觉。
  4. 当您将元素的左边缘向左拖动时,请计算其旧左侧位置和新左侧位置之间的差异,并将其添加到元素的宽度中。这将确保宽度扩展以适应新的位移。
  5. 当您将左侧边缘向右侧拖动,以缩小元素时,请执行相同的过程,只是从总宽度中减少它而不是添加它。
  6. 当您将右侧向左拖动,从而减小调整大小时,请计算差异并将其从宽度中减少。

这应该能帮助您编写代码:

通过拖动其左/上边框调整div的高度/宽度而不使用jQuery可拖动?


如果我不断减小宽度并将div从右侧移动,使其看起来像调整大小的效果,会怎么样? - Asim Khan
@AsimKhan 然后你计算差异并从宽度中减去。记住,关键在于计算新位置和旧位置之间的差异,并根据你移动的方向添加或减去。稍微想象一下,你就会明白! - Rutwick Gangurde

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接