javascript - How to drag a scaled element
问题描述
I'm making a dragged image. It works as it should. I also have buttons that scale the image. after scaling, the drug works but it cuts the position. I can't find the right words to describe this behavior.At the first movement the image changes its position slightly
what calculations do I need to do too. to get a smooth drag, after scaling an element.
https://codesandbox.io/s/laughing-tree-1zqhf?file=/src/App.js
Thank you in advance
解决方案
您必须保留初始 X 并仅计算移动的差异。
const onMouseDown = e => {
e.persist();
var initialX = e.pageX;
const onMouseMove = ev => {
var newX = initialX - ev.pageX;
setX(x - newX);
};
const onMouseUp = () => {
window.removeEventListener("mousemove", onMouseMove);
window.removeEventListener("mouseup", onMouseUp);
};
window.addEventListener("mousemove", onMouseMove);
window.addEventListener("mouseup", onMouseUp);
};
分叉沙箱链接:https ://codesandbox.io/s/compassionate-ishizaka-kvmnk?file=/src/App.js
推荐阅读
- json - 将来自多行的 JSON 对象组合成一个对象
- powerbi - 如何通过 DAX 获得季度平均值
- entity-framework - EF Core 3.0 上的 EF Core 2.2 LINQ 查询问题
- css - 如何增加 mat-option 单元格大小
- c# - 静态变量会在方法调用之间在 ASP.NET MVC RestAPI 中保持活动状态吗?
- javascript - 在异步函数上调用 await 会引发错误
- php - php - cookie 的 empty() 函数不起作用
- tcp - scapy kamene 发送 SYN 数据包
- qt - 如何转换 Qt Quick UI 表单中的项目?
- java - 为什么日期时间 1-1-1970 00:00:00 的毫秒数为负数?