【问题标题】:Drag & drop with react使用 React 拖放
【发布时间】:2020-02-10 09:53:49
【问题描述】:

我正在使用react-draggable 来实现拖放功能。

我必须将一个组件 (A) 拖到另一个 (B) 中:如何获得与 B 相关的最终 A 位置?

【问题讨论】:

    标签: javascript reactjs drag-and-drop


    【解决方案1】:

    计算每个与屏幕相关的位置并比较

    const aRect = a.getBoundingClientRect();
    const bRect = b.getBoundingClientRect();
    
    if(aRect.right > bRect.right) {
        // a start after b
    }
    if(aRect.top > bRect.top) {
       // a start under b
    }
    
    // etc.
    

    【讨论】:

    【解决方案2】:

    您可以通过使用 document.getElementById('') 获取 transform 和 translate 属性来获取组件 A 的位置,并获取 transform 和 translate。 您需要创建一个函数来获取可拖动的 css onDrag。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 2019-04-26
      • 2022-01-20
      • 2020-12-20
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      相关资源
      最近更新 更多