css 使用纯JS从左到右调整框宽度

oxiaedzo  于 2023-07-01  发布在  其他
关注(0)|答案(2)|浏览(159)

我正在尝试使一个框移动和调整大小。
我已经完善了移动部分和调整大小的权利和底部。
我有一个问题,调整左和顶部。
这是我的初始变量……

var mousePosition;
var offset;
var wrSizeOffset, wlSizeOffset, hbSizeOffset;
var div;
var isDown = false;
var wlSizeDown, wrSizeDown, htSizeDown, hbSizeDown = false;
var divWidth, divHeight;

然后我检查鼠标是否按下了盒子。

div.addEventListener('mousedown', function(e){

   isDown = true;

   offset = {
      x: div.offsetLeft - e.clientX,
      y: div.offsetTop - e.clientY
   }

   // get width without px
   divWidth = parseInt(div.style.width, 10);

});

然后我检查鼠标移动像这样...

document.addEventListener('mousemove', function(e){

mousePosition = {
  x: e.clientX,
  y: e.clientY
}

// move div while mouse is down
if(isDown){
  div.style.left = (mousePosition.x + offset.x) + 'px';
  div.style.top = (mousePosition.y + offset.y) + 'px';
}

// resize the div from left to right
// I am having no issues here
if(wrSizeDown){
  div.style.width = divWidth + mousePosition.x - wrSizeOffset.x + 'px';
}

// resizing the div from right to left
// here is where I am having an issue ...
if(wlSizeDown){
  div.style.width = divWidth - (mousePosition.x - wlSizeOffset.x) + 'px';
  // specially the below line of code
  div.style.left = divWidth + (mousePosition.x - wlSizeOffset.x) + 'px';
}

});

每当我从右到左调整大小时,div就会跳来跳去,就像它在添加样式一样。
我该怎么做?
这里有一个例子。。

var mousePosition;
var offset;
var wrSizeOffset, wlSizeOffset, hbSizeOffset;
var div;
var isDown = false;
var wlSizeDown, wrSizeDown, htSizeDown, hbSizeDown = false;
var divWidth, divHeight;

div = document.createElement('div');
div.style.position = 'relative';
div.style.left = '100px';
div.style.top = '100px';
div.style.width = '100px';
div.style.height = '100px';
div.style.backgroundColor = 'red';
div.style.border = '1px solid black';

wrSize = document.createElement('div');
wrSize.style.position = 'absolute';
wrSize.style.right = '-10px';
wrSize.style.top = '40px';
wrSize.style.width = '20px';
wrSize.style.height = '20px';
wrSize.style.backgroundColor = 'black';
wrSize.style.border = '1px solid black';

wlSize = document.createElement('div');
wlSize.style.position = 'absolute';
wlSize.style.left = '-10px';
wlSize.style.top = '40px';
wlSize.style.width = '20px';
wlSize.style.height = '20px';
wlSize.style.backgroundColor = 'black';
wlSize.style.border = '1px solid black';

htSize = document.createElement('div');
htSize.style.position = 'absolute';
htSize.style.left = '40px';
htSize.style.top = '-10px';
htSize.style.width = '20px';
htSize.style.height = '20px';
htSize.style.backgroundColor = 'black';
htSize.style.border = '1px solid black';

hbSize = document.createElement('div');
hbSize.style.position = 'absolute';
hbSize.style.left = '40px';
hbSize.style.bottom = '-10px';
hbSize.style.width = '20px';
hbSize.style.height = '20px';
hbSize.style.backgroundColor = 'black';
hbSize.style.border = '1px solid black';

document.body.appendChild(div);
div.appendChild(wrSize);
div.appendChild(wlSize);
div.appendChild(htSize);
div.appendChild(hbSize);

div.addEventListener('mousedown', function(e){

  isDown = true;

  offset = {
    x: div.offsetLeft - e.clientX,
    y: div.offsetTop - e.clientY
  }

});

wrSize.addEventListener('mousedown', function(e){

  wrSizeDown = true;

  wrSizeOffset = {
    x: e.clientX,
    y: e.clientY
  }

  divWidth = parseInt(div.style.width, 10);

});

wlSize.addEventListener('mousedown', function(e){

  wlSizeDown = true;

  wlSizeOffset = {
    x: e.clientX,
    y: e.clientY
  }

  divWidth = parseInt(div.style.width, 10);

  console.log(divWidth);
  console.log(wlSizeOffset.x);

});

hbSize.addEventListener('mousedown', function(e){

  hbSizeDown = true;

  hbSizeOffset = {
    x: e.clientX,
    y: e.clientY
  }

  divHeight = parseInt(div.style.height, 10);

});

document.addEventListener('mouseup', function(e){

  isDown = false;
  wrSizeDown = false;
  wlSizeDown = false;
  hbSizeDown = false;

});

document.addEventListener('mousemove', function(e){

  mousePosition = {
    x: e.clientX,
    y: e.clientY
  }

  if(isDown && (!wrSizeDown && !wlSizeDown && !hbSizeDown)){
    div.style.left = (mousePosition.x + offset.x) + 'px';
    div.style.top = (mousePosition.y + offset.y) + 'px';
  }

  if(wrSizeDown){
    div.style.width = divWidth + mousePosition.x - wrSizeOffset.x + 'px';
  }

  if(hbSizeDown){
    div.style.height = divHeight + mousePosition.y - hbSizeOffset.y + 'px';
  }

  if(wlSizeDown){
    div.style.width = divWidth - wlSizeOffset.x + mousePosition.x + 'px';
    div.style.left = divWidth + wlSizeOffset.x - mousePosition.x + 'px';
  }

});
body {
  margin: 0px;
}

* {
  box-sizing: border-box;
}

#box{
  position: absolute;
  display: block;
  top: 50px;
  left: 50px;
  width: 200px;
  height: 200px;
  background-color: #d80000;
  border: 1px solid #000;
}
dl5txlt9

dl5txlt91#

我根本没有看到顶部的事件监听器。但是当你按住wlSizeDown时,你需要像这样计算left:
div.style.left = divWidth - (wlSizeOffset.x - mousePosition.x) + 'px';
并为htSizeDown创建事件监听器:

div.style.height = divHeight - mousePosition.y + htSizeOffset.y + 'px';
div.style.top = divHeight + mousePosition.y - htSizeOffset.y + 'px';
omqzjyyz

omqzjyyz2#

我已经找到解决办法了。
这里是代码。。

var mousePosition;
var offset;
var isDown = false;
var isResize = false;
var div, resize;
var divWidth, divHeight;
var originalWidth, originalHeight; // added this too

div = document.createElement('div');
resize = document.createElement('div');

div.setAttribute('id', 'div');
resize.setAttribute('id', 'resize');

document.body.appendChild(div);
div.appendChild(resize);

div.addEventListener('mousedown', function(e){

  isDown = true;

  offset = {
    x: div.offsetLeft - e.clientX,
    y: div.offsetTop - e.clientY
  }

});

document.addEventListener('mouseup', function(e){

  isDown = false;
  isResize = false;

});

resize.addEventListener('mousedown', function(e){

  isResize = true;

  resizeOffset = {
    x: e.clientX,
    y: e.clientY
  }

  divWidth = div.getBoundingClientRect().width;
  originalWidth = div.getBoundingClientRect().left; // here is where i missed up

});

document.addEventListener('mousemove', function(e){

  mousePosition = {
    x: e.clientX,
    y: e.clientY
  }

  if(isDown && !isResize){
    div.style.left = (mousePosition.x + offset.x) + 'px';
    div.style.top = (mousePosition.y + offset.y) + 'px';
  }

  if(isResize){
    div.style.width = divWidth - mousePosition.x + resizeOffset.x + 'px';
    div.style.left = originalWidth + mousePosition.x - resizeOffset.x + 'px'; // added originalWidth instead of divWidth
  }

});
body{
  margin: 0px;
}

*{
  box-sizing: border-box;
}

#div{
  position: relative;
  display: block;
  top: 50px;
  left: 50px;
  width: 200px;
  height: 200px;
  background-color: #ce4257;
  border: 5px solid #000;
  border-radius: 5px;
}

#resize{
  position: absolute;
  display: block;
  top: calc(50% - 30px);
  left: -15px;
  width: 30px;
  height: 30px;
  background-color: #000;
  border-radius: 5px;
}

请阅读评论,了解我做了什么。
如果您遇到问题,了解解决方案,请发表评论,我会帮助。
谢谢你,法里斯

相关问题