【问题标题】:Drag and drop in RaphaelJS在 RaphaelJS 中拖放
【发布时间】:2015-09-25 22:19:13
【问题描述】:

我有以下小提琴: http://jsfiddle.net/zugzq7vv/

我要做的是能够将输入框左侧的数字1拖到正方形的中心,使正方形变为黑色,数字变为白色。

RaphaelJS 2.1 如何做到这一点?

JS代码:

// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(document.getElementById("papercanvas"), 320, 200);

var circle = paper.rect(50, 40, 50, 50);
// Sets the fill attribute of the circle to red (#f00)
circle.attr("fill", "#f00");

// Sets the stroke attribute of the circle to white
circle.attr("stroke", "#fff");

var t = paper.text(75, 65,""); //I want this empty string to be a text I drag

还有简单的 HTML:

1<input id="teste" disabled></input>
<div id="papercanvas"></div>

我知道拖放界面,但我似乎无法将其应用于单个数字。

【问题讨论】:

标签: javascript raphael


【解决方案1】:

您不能使用 Raphael.js 使非 svg/Raphael 元素拖动,即输入元素之前的字符串“1”。所以为此你需要有Raphael-text 元素。

尽管Raphael 支持onDragOver 功能,但还不足以完全满足您的要求。比如,它可以在元素结束时触发,但它没有任何像 onOut 这样的 api,因此您可以恢复颜色/状态。

因此,正如我在其他 SO answer 中所解释的,我们需要跟踪坐标,并基于此在 onDragComplete 方法中采取行动。

这是您所要求的工作fiddle。但是,随着它的扩展,您需要在其中添加更多逻辑来处理。

var text = paper.text(10, 10, "1");
text.attr('cursor','pointer');
text.attr('font-size', '20px');

text.drag(onDragMove, onDragStart, onDragComplete);

function onDragStart(){
    this.ox = this.attr('x');
    this.oy = this.attr('y');    
}

function onDragMove(dx,dy){
    this.attr({x: this.ox + dx, y: this.oy + dy });
}

function onDragComplete(){
    if((this.attr('x') > 20 && (this.attr('x') < 70)) && (this.attr('y') < 50)) {
        this.attr('fill', 'white');
        rect.attr('fill', 'black');
    } else {
        this.attr('fill', 'black');
        rect.attr('fill', 'red');
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多