javascript - d3js 防止拖拽点击事件

标签 javascript d3.js svg

我正在尝试在 d3js 中绘制一个 UI,其中我有一个顶部面板,我应该能够dragdrop 元素到一个组中并复制它们。我已经实现了那个目标,但是我的代码中有一个错误。我真正想做的是,拖动 circle 并复制它。但是,当我单击顶部面板中的 circle 时,它会自动触发拖动事件并自行复制。我怎样才能阻止这种行为?

<svg height="600" width="600" style="background: black">

    <g>
        <rect x="0" y="0" , width="600" height="40" style="fill:gold;"></rect>
        <circle id='drag' cx="15" cy="20" init-cx="15" init-cy="20" r="10"
                style="stroke: white; stroke-width: 2px; fill:blue"/>

    </g>

    <g id="playground">
        <g>
            <circle id='top' cx="180" cy="120" r="30" style="stroke: white; stroke-width: 2px; fill:white"/>
        </g>
        <g>
            <circle id='top' cx="200" cy="220" r="30" style="stroke: white; stroke-width: 2px; fill:white"/>
        </g>
        <g>
            <circle id='top' cx="320" cy="150" r="50" style="stroke: white; stroke-width: 2px; fill:white"/>
        </g>
    </g>
</svg>

<script>

    $(document).ready(function () {


        var move = d3.behavior.drag()
            .on('drag', function () {

                console.log('dragging');

                var curr = d3.select(this)
                    .attr({
                        cx: d3.mouse(this)[0],
                        cy: d3.mouse(this)[1]
                    })


            })
            .on('dragend', function () {

                var curr = d3.select(this);

                d3.select('#playground')
                    .append('circle')
                    .attr({
                        cx : curr.attr('cx'),
                        cy : curr.attr('cy'),
                        r : curr.attr('r')
                    })
                    .style({
                        fill : 'white',
                        stroke : 'red',
                        'stroke-width' : '2px'
                    })
                ;

                curr.attr({
                    cx : curr.attr('init-cx'),
                    cy : curr.attr('init-cx')
                });
            })
            ;


        d3.select('#drag').call(move);


    });


</script>

这是我的作品。 https://jsfiddle.net/fawzan/my2g724L/

最佳答案

希望对你有用 看一看。 我在“dragend”中添加了很少的代码,在这里我决定是否通过使用 circle 的属性(如 init-cx 和 init-cy)来创建/附加一个圆到 Playground 。我添加的代码是

var initX = (curr.attr('init-cx')*1);
var currX = (curr.attr('cx')*1);
var initY = (curr.attr('init-cy')*1);
var currY = (curr.attr('cy')*1);                  
if(((currX) > (initX+20)) || ((currY) > (initY+20))){
//Here code to append a circle to playground
}

Fiddle

:D

关于javascript - d3js 防止拖拽点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908765/

相关文章:

javascript - 在同一选项卡与新选项卡中开始下载的原因?

javascript - 如何使用 cypher 查询更新 Neo4j 中的关系详细信息?

javascript - 如何创建和使用动态分层 JSON?

css - 来自 svg 蒙版的不需要的水平线

javascript - 预期 "payload"是一个普通对象 : MEAN

javascript - 基数插值曲线下面积

javascript - 使用 D3 绘制条形图中的 Time.Scale 和 RangeBand

javascript - 在 Jupyter Notebook 中使用 ipywidgets DOMWidget 修复 SVG 显示损坏

javascript - Raphael SVG 动画在某些浏览器中不稳定

javascript - 在对象数组的开头追加数组 - Javascript