【问题标题】:Drag a div and see the item moving拖动一个 div 并查看项目移动
【发布时间】:2015-07-03 16:30:45
【问题描述】:

现在我正在尝试实现拖放功能。

我有这个。

var drag = d3.behavior.drag();
  drag.on('drag', function () {
    console.log("drag start");
  })
  drag.on('dragend', function () {
    console.log("drag stop");
  })

  selection = d3.select('.right.menu');
  selection.call(drag);

这可行,但现在我想看到 div 在屏幕上移动,现在我只得到控制台,如何做到这一点,或者更好的是关于如何命名此方法/事件以进行一些研究的任何指导会很棒。

更新

当前解决方案。

var drag = d3.behavior.drag();
  drag.on('drag', function () {
console.log(selection);
    selection.style("left", d3.event.x+"px").style("top", d3.event.y+"px").style("position", "inherit");
  })
  drag.on('dragend', function () {
    console.log("drag stop");
  })

  selection = d3.select('.right.menu')
  selection.call(drag);

【问题讨论】:

    标签: javascript d3.js drag-and-drop drag


    【解决方案1】:

    这是一个简单的例子,使用 css position:absolute 移动一个 div 并根据拖动修改顶部和左侧的偏移量。

    appdiv.style("left", d3.event.x+"px").style("top", d3.event.y+"px");
    

    Fiddle

    【讨论】:

    • 谢谢,用当前解决方案更新了答案
    猜你喜欢
    • 1970-01-01
    • 2013-10-04
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多