用纯JS从左到右调整框的宽度大小。

huangapple go评论74阅读模式
英文:

Resizing a box width from left to right using pure JS

问题

I am trying to make a box move and resize.
我正在尝试让一个框移动和调整大小。

I have perfected the moving part and the resizing for the right and bottom.
我已经完善了移动部分和右侧和底部的调整大小部分。

I am having an issue resizing left and top.
我在调整左侧和顶部时遇到问题。

Here are my initial vars ...
以下是我的初始变量...

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

After that I check whether the box is on mouse down ...
之后,我检查鼠标是否按下了框...

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);

});

And then I check mouse on move like so ...
然后我检查鼠标的移动...

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';
}

});

Whenever I resize from right to left the div jumps around like it's adding to the style.left
每当我从右侧调整大小到左侧时,框会像添加到style.left一样跳动。

How can I make this work?
我该如何使其工作?

Here is an example ...
以下是一个示例...

英文:

I am trying to make a box move and resize.

I have perfected the moving part and the resizing for the right and bottom.

I am having an issue resizing left and top.

Here are my initial vars ...

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

After that I check whether the box is on mouse down ...

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);

});

And then I check mouse on move like so ...

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';
}

});

Whenever I resize from right to left the div jumps around like it's adding to the style.left

How can I make this work?

Here is an example ...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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(&#39;div&#39;);
div.style.position = &#39;relative&#39;;
div.style.left = &#39;100px&#39;;
div.style.top = &#39;100px&#39;;
div.style.width = &#39;100px&#39;;
div.style.height = &#39;100px&#39;;
div.style.backgroundColor = &#39;red&#39;;
div.style.border = &#39;1px solid black&#39;;

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

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

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

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

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

div.addEventListener(&#39;mousedown&#39;, function(e){

  isDown = true;

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

});

wrSize.addEventListener(&#39;mousedown&#39;, function(e){

  wrSizeDown = true;

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

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

});

wlSize.addEventListener(&#39;mousedown&#39;, 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(&#39;mousedown&#39;, function(e){

  hbSizeDown = true;

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

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

});

document.addEventListener(&#39;mouseup&#39;, function(e){

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

});

document.addEventListener(&#39;mousemove&#39;, function(e){

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

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

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

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

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

});

<!-- language: lang-css -->

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;
}

<!-- end snippet -->

答案1

得分: 1

我不看到顶部的事件监听器。但是当你按住wlSizeDown时,需要这样计算左边:

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';
英文:

I don't see an eventlistener for the top at all. But when you're holding down wlSizeDown, you need to calculate left like so:

div.style.left = divWidth - (wlSizeOffset.x - mousePosition.x) + &#39;px&#39;;

and for htSizeDown event listener is created:

div.style.height = divHeight - mousePosition.y + htSizeOffset.y + &#39;px&#39;;
div.style.top = divHeight + mousePosition.y - htSizeOffset.y + &#39;px&#39;;

</details>



# 答案2
**得分**: 0

我已找到解决方案。

以下是代码...

```javascript
var mousePosition;
var offset;
var isDown = false;
var isResize = false;
var div, resize;
var divWidth, divHeight;
var originalWidth, originalHeight; // 加入了这一行

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; // 在这里我弄错了

});

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'; // 我添加了originalWidth而不是divWidth
  }

});
```

```css
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;
}
```

请阅读注释以了解我所做的更改。

如果您在理解解决方案方面遇到问题,请留下评论,我会提供帮助。

谢谢,
Farris

<details>
<summary>英文:</summary>

I have found the solution.

Here is the code ...

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    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(&#39;div&#39;);
    resize = document.createElement(&#39;div&#39;);

    div.setAttribute(&#39;id&#39;, &#39;div&#39;);
    resize.setAttribute(&#39;id&#39;, &#39;resize&#39;);

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

    div.addEventListener(&#39;mousedown&#39;, function(e){

      isDown = true;

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

    });

    document.addEventListener(&#39;mouseup&#39;, function(e){

      isDown = false;
      isResize = false;

    });

    resize.addEventListener(&#39;mousedown&#39;, 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(&#39;mousemove&#39;, function(e){

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

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

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

    });

&lt;!-- language: lang-css --&gt;

    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;
    }

&lt;!-- end snippet --&gt;

Please read the comments to understand what I have done.

If you are facing problems understanding the solution please comment and I will help.

Thank you,
Farris

</details>



huangapple
  • 本文由 发表于 2023年6月29日 07:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定