鱼C论坛

 找回密码
 立即注册
查看: 471|回复: 6

[已解决]html问题

[复制链接]
发表于 2023-8-16 11:35:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. #test {
  5.   position: absolute;
  6.   padding: 10px;
  7.   cursor: move;
  8.   z-index: 10;
  9. }
  10. </style>
  11. <body>

  12. <div id="test">test</div>

  13. <script>
  14. let elmnt = document.getElementById("test");
  15. let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  16. elmnt.onmousedown = mousedown;
  17. function mousedown(e) {
  18.   e = e || window.event;
  19.   e.preventDefault();
  20.   pos3 = e.screenX;
  21.   pos4 = e.screenY;
  22.   document.onmouseup = function(){
  23.         document.onmousedown = null;
  24.         document.onmousemove = null;
  25.   };
  26.   document.onmousemove = drag;
  27. }
  28. function drag(e) {
  29.   e = e || window.event;
  30.   e.preventDefault();
  31.   pos3 = e.screenX;
  32.   pos4 = e.screenY;
  33.   elmnt.style.top = (pos3) + "px";
  34.   elmnt.style.left = (pos4) + "px";
  35. }
  36. </script>

  37. </body>
  38. </html>

复制代码


动画.gif

啊啊啊到底是哪里错了
最佳答案
2023-8-16 12:31:51
出现错误的原因是在drag函数中将pos3赋值给了elmnt.style.top,将pos4赋值给了elmnt.style.left,导致元素在移动时位置改变不正确。
你可以修改drag函数中的代码:
  1. function drag(e) {
  2.   e = e || window.event;
  3.   e.preventDefault();
  4.   let newPos1 = e.screenX - pos3;
  5.   let newPos2 = e.screenY - pos4;
  6.   pos3 = e.screenX;
  7.   pos4 = e.screenY;
  8.   elmnt.style.top = (elmnt.offsetTop + newPos2) + "px";
  9.   elmnt.style.left = (elmnt.offsetLeft + newPos1) + "px";
  10. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-16 11:35:39 | 显示全部楼层
在你的代码中,你想要通过鼠标拖拽来移动一个元素。然而,你的代码中有一个错误。在  drag  函数中,你应该将  pos3  赋给  elmnt.style.left ,将  pos4  赋给  elmnt.style.top ,而不是相反。

这是修正后的代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. #test {
  5.   position: absolute;
  6.   padding: 10px;
  7.   cursor: move;
  8.   z-index: 10;
  9. }
  10. </style>
  11. <body>

  12. <div id="test">test</div>

  13. <script>
  14. let elmnt = document.getElementById("test");
  15. let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  16. elmnt.onmousedown = mousedown;

  17. function mousedown(e) {
  18.   e = e || window.event;
  19.   e.preventDefault();
  20.   pos3 = e.screenX;
  21.   pos4 = e.screenY;
  22.   document.onmouseup = function(){
  23.        document.onmousedown = null;
  24.        document.onmousemove = null;
  25.   };
  26.   document.onmousemove = drag;
  27. }

  28. function drag(e) {
  29.   e = e || window.event;
  30.   e.preventDefault();
  31.   pos1 = pos3 - e.screenX;
  32.   pos2 = pos4 - e.screenY;
  33.   pos3 = e.screenX;
  34.   pos4 = e.screenY;
  35.   elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  36.   elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  37. }
  38. </script>

  39. </body>
  40. </html>
复制代码


这样,你应该能够通过鼠标拖拽来移动元素了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-16 12:08:39 | 显示全部楼层
xy反了吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-16 12:11:00 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-16 12:13:49 | 显示全部楼层
我还没有学到这呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-16 12:31:51 | 显示全部楼层    本楼为最佳答案   
出现错误的原因是在drag函数中将pos3赋值给了elmnt.style.top,将pos4赋值给了elmnt.style.left,导致元素在移动时位置改变不正确。
你可以修改drag函数中的代码:
  1. function drag(e) {
  2.   e = e || window.event;
  3.   e.preventDefault();
  4.   let newPos1 = e.screenX - pos3;
  5.   let newPos2 = e.screenY - pos4;
  6.   pos3 = e.screenX;
  7.   pos4 = e.screenY;
  8.   elmnt.style.top = (elmnt.offsetTop + newPos2) + "px";
  9.   elmnt.style.left = (elmnt.offsetLeft + newPos1) + "px";
  10. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-16 13:57:30 | 显示全部楼层
应该是x, y坐标反了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 00:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表