我需要拖动一个表格单元格并在拖动时在鼠标指针下显示其鬼影。该表完全由d3.js生成。当我在适当的单元格上将draggable属性设置为true时,重影图像将按预期显示。
rows
.append("td")
.text(function (d) {return d.name;})
.attr('draggable', 'true');
但是由于我需要拖动来影响页面上的其他元素(设置不同的样式,过滤器选择等),我调用d3.behavior.drag()并实现dragstart,drag和dragend函数。
.call(d3.behavior.drag()
.on('dragstart', function () {
d3.select(this)
.classed('dragged', true)
.style('font-style', 'italic')
.style('color', 'grey');
})
.on('drag', function () {
d3.select('#drop-target')
.style('background-color', 'green');
})
.on('dragend', function () {
d3.select(this)
.classed('dragged', false)
.style('font-style', 'normal')
.style('color', 'black');
d3.select('#drop-target')
.style('background-color', 'yellow');
})
);
问题是d3.behavior.drag()似乎覆盖了鬼影像的创建,现在没有可视化队列来拖动元素。我究竟做错了什么?
答案 0 :(得分:1)
而不是将监听器注册到drag behavior
,而不是将其添加到选择中。
rows.append('td').html(function (d) {
return d.type;
})
.style('font-style', 'italic');
rows.append('td').text(function (d) {
return d.name;
})
.attr('draggable', 'true')
.on('dragstart', function () {
d3.select(this)
.classed('dragged', true)
.style('font-style', 'italic')
.style('color', 'grey');
})
.on('drag', function () {
d3.select('#drop-target')
.style('background-color', 'green');
})
.on('dragend', function () {
d3.select(this)
.classed('dragged', false)
.style('font-style', 'normal')
.style('color', 'black');
d3.select('#drop-target')
.style('background-color', 'yellow');
});
工作代码here
希望这有帮助!