【发布时间】:2013-11-02 18:31:28
【问题描述】:
问题是关于新发布的Snap.svg 中Element.drag 的onstart 事件处理程序。
以下代码的目的是为 svg 对象上的拖动(onstart/onstop)的开始和停止注册事件处理程序。
var s = Snap(800,600);
var bigCircle = s.circle(300,150,100);
bigCircle.drag(null,
function(){
console.log("Move started");
},
function(){
console.log("Move stopped");
}
);
控制台消息在拖动开始和停止时工作正常,但 null 会覆盖默认的 onmove 函数 - 导致没有实际拖动发生。如何传递“我不想弄乱默认的 onmove”的内容?
(注意:我更喜欢通过赋值的方式注册一个事件处理程序,就像我们熟悉的 onClick 一样,但那是另一回事。)
几个小时后添加的注释: Raphael.js 文档和示例提供了一些线索。至少现在我知道如何为 onmove 传递一个提供默认移动行为的正确函数:
var s = Snap(800,600);
var bigCircle = s.circle(300,150,100);
start = function() {
this.ox = parseInt(this.attr("cx"));
this.oy = parseInt(this.attr("cy"));
console.log("Start move, ox=" + this.ox + ", oy=" + this.oy);
}
move = function(dx, dy) {
this.attr({"cx": this.ox + dx, "cy": this.oy + dy});
}
stop = function() {
this.ox = parseInt(this.attr("cx"));
this.oy = parseInt(this.attr("cy"));
console.log("Stop move, ox=" + this.ox + ", oy=" + this.oy);
}
bigCircle.drag(move, start, stop);
【问题讨论】:
-
是的,我遇到了同样的问题,最后到了这里,如果我想附加一个 movestart 处理程序,我必须重新定义 onmove 函数是没有意义的
标签: javascript svg snap.svg