通过鼠标单击调整多个div的大小

时间:2019-05-17 20:57:39

标签: javascript html css

我看了这篇文章,我想在更多的div上尝试同样的技术。

以上代码适用于2格,但不适用于4格。我试图找出原因,因此决定尝试以下代码。

var handler = document.querySelector('.handler');
var wrapperWidth;
var wrapper = handler.closest('.wrapper');
var box = wrapper.querySelector('.box');
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 containerOffsetLeft = wrapper.offsetLeft;

  // Get x-coordinate of pointer relative to container
  var pointerRelativeXpos = e.clientX - containerOffsetLeft;
  
  // Arbitrary minimum width set on box A, otherwise its inner content will collapse to width of 0
  var boxAminWidth = 60;

  // Resize box A
  // * 8px is the left/right spacing between .handler and its inner pseudo-element
  // * Set flex-grow to 0 to prevent it from growing
  
  wrapperWidth = wrapper.stlye.width;
  
  box.style.width = (Math.max(boxAminWidth, wrapperWidth - 8)) + 'px';
  box.style.flexGrow = 0;
});

document.addEventListener('mouseup', function(e) {
  // Turn off dragging flag when user mouse is up
  isHandlerDragging = false;
});
.wrapper {
  background-color: #fff;
  color: #444;
  /* Use flexbox */
  display: flex;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  
  /* 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 */
  flex: 1 1 1 1 auto;
}

.handler {
  width: 20px;
  padding: 0;
  cursor: ew-resize;
  flex: 0 0 auto;
}

.handler::before {
  content: '';
  display: block;
  width: 4px;
  height: 100%;
  background: red;
  margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="wrapper">
  <div class="box">A</div>
  <div class="handler"></div>
  <div class="box">B</div>
   <div class="handler"></div>
  <div class="box">C</div>
   <div class="handler"></div>
  <div class="box">D</div>
</div>
</body>
</html>

我想发生的事情是让div可安排。

您可以在此处查看代码。

https://jsfiddle.net/paralaxwombat/1Lfqdb6x/

1 个答案:

答案 0 :(得分:3)

如果这是您想要的

var wrapper = document.querySelector('.wrapper');
var box = null;
var isHandlerDragging = false;
var boxAminWidth = 60;
var new_width = 0, current_width = 0;

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

document.addEventListener('mousemove', function(e) {
  // Don't do anything if dragging flag is false or 
  if (!isHandlerDragging) {
    return false;
  }
  
  // save the current box width
 	current_width = box.style.width;
  
  // check the minimum width
 if ((new_width = e.clientX - box.offsetLeft - 8 ) >= boxAminWidth) {
 	box.style.width = new_width + 'px';
 }
  
  // make sure the boxs dont go past the wrapper, aka: the overflow effect
  //if they do, we recover the last width of the current box to keep the boxs inside the wrapper.
  if(wrapper.lastElementChild.offsetLeft + wrapper.lastElementChild.offsetWidth > wrapper.offsetWidth) {
  box.style.width = current_width;
  }

});

document.addEventListener('mouseup', function(e) {
  // Turn off dragging flag when user mouse is up
  isHandlerDragging = false;
});
.wrapper {
  background-color: #fff;
  color: #444;
  /* Use flexbox */
  display: flex;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  
  /* 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 */
  flex: 1 1 1 1 auto;
  
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   
   flex-grow: 0;
   flex-shrink: 0;
}

.handler {
  width: 20px;
  padding: 0;
  cursor: ew-resize;
  flex: 0 0 auto;
}

.handler::before {
  content: '';
  display: block;
  width: 4px;
  height: 100%;
  background: red;
  margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="wrapper">
  <div class="box">A</div>
  <div class="handler"></div>
  <div class="box">B</div>
   <div class="handler"></div>
  <div class="box">C</div>
   <div class="handler"></div>
  <div class="box">D</div>
</div>
</body>
</html>

我有一些发现:

var handler = document.querySelector('.handler');

此行代码(与jQuery不同)仅选择第一个处理程序,而不是全部选择处理程序,因此此检查if (e.target === handler)仅对第一个处理程序有效,因此mousemove会被选中。不能对所有人都起作用。 var box = wrapper.querySelector('.box');也是如此,您将始终在第一个方框中设置。

这是新的javaScript代码

var wrapper = document.querySelector('.wrapper');
var box = null;
var isHandlerDragging = false;
var boxAminWidth = 60;
var new_width = 0, current_width = 0;

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

});

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

  // save the current box width
    current_width = box.style.width;

  // check the minimum width
 if ((new_width = e.clientX - box.offsetLeft - 8 ) >= boxAminWidth) {
    box.style.width = new_width + 'px';
 }

  // make sure the boxs dont go past the wrapper, aka: the overflow effect
  //if they do, we recover the last width of the current box to keep the boxs inside the wrapper.
  if(wrapper.lastElementChild.offsetLeft + wrapper.lastElementChild.offsetWidth > wrapper.offsetWidth) {
  box.style.width = current_width;
  }

});

document.addEventListener('mouseup', function(e) {
  // Turn off dragging flag when user mouse is up
  isHandlerDragging = false;
});

在CSS中,我对box类做了一些小的改动:

.box {
   /* ... */
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   flex-grow: 0;
   flex-shrink: 0;
}