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

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

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 ...

  1. var mousePosition;
  2. var offset;
  3. var wrSizeOffset, wlSizeOffset, hbSizeOffset;
  4. var div;
  5. var isDown = false;
  6. var wlSizeDown, wrSizeDown, htSizeDown, hbSizeDown = false;
  7. var divWidth, divHeight;

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

  1. div.addEventListener('mousedown', function(e){
  2. isDown = true;
  3. offset = {
  4. x: div.offsetLeft - e.clientX,
  5. y: div.offsetTop - e.clientY
  6. }
  7. // get width without px
  8. divWidth = parseInt(div.style.width, 10);
  9. });

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

  1. document.addEventListener('mousemove', function(e){
  2. mousePosition = {
  3. x: e.clientX,
  4. y: e.clientY
  5. }
  6. // move div while mouse is down
  7. if(isDown){
  8. div.style.left = (mousePosition.x + offset.x) + 'px';
  9. div.style.top = (mousePosition.y + offset.y) + 'px';
  10. }
  11. // resize the div from left to right
  12. // I am having no issues here
  13. if(wrSizeDown){
  14. div.style.width = divWidth + mousePosition.x - wrSizeOffset.x + 'px';
  15. }
  16. // resizing the div from right to left
  17. // here is where I am having an issue ...
  18. if(wlSizeDown){
  19. div.style.width = divWidth - (mousePosition.x - wlSizeOffset.x) + 'px';
  20. // specially the below line of code
  21. div.style.left = divWidth + (mousePosition.x - wlSizeOffset.x) + 'px';
  22. }
  23. });

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

  1. var mousePosition;
  2. var offset;
  3. var wrSizeOffset, wlSizeOffset, hbSizeOffset;
  4. var div;
  5. var isDown = false;
  6. var wlSizeDown, wrSizeDown, htSizeDown, hbSizeDown = false;
  7. var divWidth, divHeight;
  8. div = document.createElement(&#39;div&#39;);
  9. div.style.position = &#39;relative&#39;;
  10. div.style.left = &#39;100px&#39;;
  11. div.style.top = &#39;100px&#39;;
  12. div.style.width = &#39;100px&#39;;
  13. div.style.height = &#39;100px&#39;;
  14. div.style.backgroundColor = &#39;red&#39;;
  15. div.style.border = &#39;1px solid black&#39;;
  16. wrSize = document.createElement(&#39;div&#39;);
  17. wrSize.style.position = &#39;absolute&#39;;
  18. wrSize.style.right = &#39;-10px&#39;;
  19. wrSize.style.top = &#39;40px&#39;;
  20. wrSize.style.width = &#39;20px&#39;;
  21. wrSize.style.height = &#39;20px&#39;;
  22. wrSize.style.backgroundColor = &#39;black&#39;;
  23. wrSize.style.border = &#39;1px solid black&#39;;
  24. wlSize = document.createElement(&#39;div&#39;);
  25. wlSize.style.position = &#39;absolute&#39;;
  26. wlSize.style.left = &#39;-10px&#39;;
  27. wlSize.style.top = &#39;40px&#39;;
  28. wlSize.style.width = &#39;20px&#39;;
  29. wlSize.style.height = &#39;20px&#39;;
  30. wlSize.style.backgroundColor = &#39;black&#39;;
  31. wlSize.style.border = &#39;1px solid black&#39;;
  32. htSize = document.createElement(&#39;div&#39;);
  33. htSize.style.position = &#39;absolute&#39;;
  34. htSize.style.left = &#39;40px&#39;;
  35. htSize.style.top = &#39;-10px&#39;;
  36. htSize.style.width = &#39;20px&#39;;
  37. htSize.style.height = &#39;20px&#39;;
  38. htSize.style.backgroundColor = &#39;black&#39;;
  39. htSize.style.border = &#39;1px solid black&#39;;
  40. hbSize = document.createElement(&#39;div&#39;);
  41. hbSize.style.position = &#39;absolute&#39;;
  42. hbSize.style.left = &#39;40px&#39;;
  43. hbSize.style.bottom = &#39;-10px&#39;;
  44. hbSize.style.width = &#39;20px&#39;;
  45. hbSize.style.height = &#39;20px&#39;;
  46. hbSize.style.backgroundColor = &#39;black&#39;;
  47. hbSize.style.border = &#39;1px solid black&#39;;
  48. document.body.appendChild(div);
  49. div.appendChild(wrSize);
  50. div.appendChild(wlSize);
  51. div.appendChild(htSize);
  52. div.appendChild(hbSize);
  53. div.addEventListener(&#39;mousedown&#39;, function(e){
  54. isDown = true;
  55. offset = {
  56. x: div.offsetLeft - e.clientX,
  57. y: div.offsetTop - e.clientY
  58. }
  59. });
  60. wrSize.addEventListener(&#39;mousedown&#39;, function(e){
  61. wrSizeDown = true;
  62. wrSizeOffset = {
  63. x: e.clientX,
  64. y: e.clientY
  65. }
  66. divWidth = parseInt(div.style.width, 10);
  67. });
  68. wlSize.addEventListener(&#39;mousedown&#39;, function(e){
  69. wlSizeDown = true;
  70. wlSizeOffset = {
  71. x: e.clientX,
  72. y: e.clientY
  73. }
  74. divWidth = parseInt(div.style.width, 10);
  75. console.log(divWidth);
  76. console.log(wlSizeOffset.x);
  77. });
  78. hbSize.addEventListener(&#39;mousedown&#39;, function(e){
  79. hbSizeDown = true;
  80. hbSizeOffset = {
  81. x: e.clientX,
  82. y: e.clientY
  83. }
  84. divHeight = parseInt(div.style.height, 10);
  85. });
  86. document.addEventListener(&#39;mouseup&#39;, function(e){
  87. isDown = false;
  88. wrSizeDown = false;
  89. wlSizeDown = false;
  90. hbSizeDown = false;
  91. });
  92. document.addEventListener(&#39;mousemove&#39;, function(e){
  93. mousePosition = {
  94. x: e.clientX,
  95. y: e.clientY
  96. }
  97. if(isDown &amp;&amp; (!wrSizeDown &amp;&amp; !wlSizeDown &amp;&amp; !hbSizeDown)){
  98. div.style.left = (mousePosition.x + offset.x) + &#39;px&#39;;
  99. div.style.top = (mousePosition.y + offset.y) + &#39;px&#39;;
  100. }
  101. if(wrSizeDown){
  102. div.style.width = divWidth + mousePosition.x - wrSizeOffset.x + &#39;px&#39;;
  103. }
  104. if(hbSizeDown){
  105. div.style.height = divHeight + mousePosition.y - hbSizeOffset.y + &#39;px&#39;;
  106. }
  107. if(wlSizeDown){
  108. div.style.width = divWidth - wlSizeOffset.x + mousePosition.x + &#39;px&#39;;
  109. div.style.left = divWidth + wlSizeOffset.x - mousePosition.x + &#39;px&#39;;
  110. }
  111. });

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

  1. body {
  2. margin: 0px;
  3. }
  4. * {
  5. box-sizing: border-box;
  6. }
  7. #box{
  8. position: absolute;
  9. display: block;
  10. top: 50px;
  11. left: 50px;
  12. width: 200px;
  13. height: 200px;
  14. background-color: #d80000;
  15. border: 1px solid #000;
  16. }

<!-- end snippet -->

答案1

得分: 1

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

div.style.left = divWidth - (wlSizeOffset.x - mousePosition.x) + 'px';

对于htSizeDown事件监听器被创建:

  1. div.style.height = divHeight - mousePosition.y + htSizeOffset.y + 'px';
  2. 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:

  1. div.style.height = divHeight - mousePosition.y + htSizeOffset.y + &#39;px&#39;;
  2. div.style.top = divHeight + mousePosition.y - htSizeOffset.y + &#39;px&#39;;
  3. </details>
  4. # 答案2
  5. **得分**: 0
  6. 我已找到解决方案。
  7. 以下是代码...
  8. ```javascript
  9. var mousePosition;
  10. var offset;
  11. var isDown = false;
  12. var isResize = false;
  13. var div, resize;
  14. var divWidth, divHeight;
  15. var originalWidth, originalHeight; // 加入了这一行
  16. div = document.createElement('div');
  17. resize = document.createElement('div');
  18. div.setAttribute('id', 'div');
  19. resize.setAttribute('id', 'resize');
  20. document.body.appendChild(div);
  21. div.appendChild(resize);
  22. div.addEventListener('mousedown', function(e){
  23. isDown = true;
  24. offset = {
  25. x: div.offsetLeft - e.clientX,
  26. y: div.offsetTop - e.clientY
  27. }
  28. });
  29. document.addEventListener('mouseup', function(e){
  30. isDown = false;
  31. isResize = false;
  32. });
  33. resize.addEventListener('mousedown', function(e){
  34. isResize = true;
  35. resizeOffset = {
  36. x: e.clientX,
  37. y: e.clientY
  38. }
  39. divWidth = div.getBoundingClientRect().width;
  40. originalWidth = div.getBoundingClientRect().left; // 在这里我弄错了
  41. });
  42. document.addEventListener('mousemove', function(e){
  43. mousePosition = {
  44. x: e.clientX,
  45. y: e.clientY
  46. }
  47. if(isDown && !isResize){
  48. div.style.left = (mousePosition.x + offset.x) + 'px';
  49. div.style.top = (mousePosition.y + offset.y) + 'px';
  50. }
  51. if(isResize){
  52. div.style.width = divWidth - mousePosition.x + resizeOffset.x + 'px';
  53. div.style.left = originalWidth + mousePosition.x - resizeOffset.x + 'px'; // 我添加了originalWidth而不是divWidth
  54. }
  55. });
  56. ```
  57. ```css
  58. body{
  59. margin: 0px;
  60. }
  61. *{
  62. box-sizing: border-box;
  63. }
  64. #div{
  65. position: relative;
  66. display: block;
  67. top: 50px;
  68. left: 50px;
  69. width: 200px;
  70. height: 200px;
  71. background-color: #ce4257;
  72. border: 5px solid #000;
  73. border-radius: 5px;
  74. }
  75. #resize{
  76. position: absolute;
  77. display: block;
  78. top: calc(50% - 30px);
  79. left: -15px;
  80. width: 30px;
  81. height: 30px;
  82. background-color: #000;
  83. border-radius: 5px;
  84. }
  85. ```
  86. 请阅读注释以了解我所做的更改。
  87. 如果您在理解解决方案方面遇到问题,请留下评论,我会提供帮助。
  88. 谢谢,
  89. Farris
  90. <details>
  91. <summary>英文:</summary>
  92. I have found the solution.
  93. Here is the code ...
  94. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  95. &lt;!-- language: lang-js --&gt;
  96. var mousePosition;
  97. var offset;
  98. var isDown = false;
  99. var isResize = false;
  100. var div, resize;
  101. var divWidth, divHeight;
  102. var originalWidth, originalHeight; // added this too
  103. div = document.createElement(&#39;div&#39;);
  104. resize = document.createElement(&#39;div&#39;);
  105. div.setAttribute(&#39;id&#39;, &#39;div&#39;);
  106. resize.setAttribute(&#39;id&#39;, &#39;resize&#39;);
  107. document.body.appendChild(div);
  108. div.appendChild(resize);
  109. div.addEventListener(&#39;mousedown&#39;, function(e){
  110. isDown = true;
  111. offset = {
  112. x: div.offsetLeft - e.clientX,
  113. y: div.offsetTop - e.clientY
  114. }
  115. });
  116. document.addEventListener(&#39;mouseup&#39;, function(e){
  117. isDown = false;
  118. isResize = false;
  119. });
  120. resize.addEventListener(&#39;mousedown&#39;, function(e){
  121. isResize = true;
  122. resizeOffset = {
  123. x: e.clientX,
  124. y: e.clientY
  125. }
  126. divWidth = div.getBoundingClientRect().width;
  127. originalWidth = div.getBoundingClientRect().left; // here is where i missed up
  128. });
  129. document.addEventListener(&#39;mousemove&#39;, function(e){
  130. mousePosition = {
  131. x: e.clientX,
  132. y: e.clientY
  133. }
  134. if(isDown &amp;&amp; !isResize){
  135. div.style.left = (mousePosition.x + offset.x) + &#39;px&#39;;
  136. div.style.top = (mousePosition.y + offset.y) + &#39;px&#39;;
  137. }
  138. if(isResize){
  139. div.style.width = divWidth - mousePosition.x + resizeOffset.x + &#39;px&#39;;
  140. div.style.left = originalWidth + mousePosition.x - resizeOffset.x + &#39;px&#39;; // added originalWidth instead of divWidth
  141. }
  142. });
  143. &lt;!-- language: lang-css --&gt;
  144. body{
  145. margin: 0px;
  146. }
  147. *{
  148. box-sizing: border-box;
  149. }
  150. #div{
  151. position: relative;
  152. display: block;
  153. top: 50px;
  154. left: 50px;
  155. width: 200px;
  156. height: 200px;
  157. background-color: #ce4257;
  158. border: 5px solid #000;
  159. border-radius: 5px;
  160. }
  161. #resize{
  162. position: absolute;
  163. display: block;
  164. top: calc(50% - 30px);
  165. left: -15px;
  166. width: 30px;
  167. height: 30px;
  168. background-color: #000;
  169. border-radius: 5px;
  170. }
  171. &lt;!-- end snippet --&gt;
  172. Please read the comments to understand what I have done.
  173. If you are facing problems understanding the solution please comment and I will help.
  174. Thank you,
  175. Farris
  176. </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:

确定