Raphael hover事件使用

来源:互联网 发布:从零开始学python pdf 编辑:程序博客网 时间:2024/05/08 10:09

Raphael hover就是鼠标悬挺事件,我用Raphael绘制了图形后,希望hover时间被触发,并能显示该图形代表的意义。

假定c是一个circle对象,

var context = {                    self: this,    circle: c,    point: pt,    lineRatio: line.ratio};//c.hover(this.onHoverNode, null, context, null);c.hover(    (function(self, ctx) {return function() {    self.onHoverNode(ctx);};    }(this, context)));

这里没有使用Raphael中的icontext参数,我也想,可是不知道怎么用,上面注释掉的那一行就是我想用但没成功的。我还是用了闭包。这样context对象就做为参数传递进来了。不过对性能不太好,特别是circle对象很多的时候。然后在onHover函数中我获取了数据,并显示出来,具体代码和业务逻辑有关,下面仅供参考:

onHoverNode: function (context) {    var self = this;    var text = self.getTimeString(context.point.x) + ", " + context.point.y.toFixed(2) + "%";    if (!self.curText) {self.curText = self.paper.text(self.toX(self.minX) + self.width / 2 - 30,       self.toY(self.minY) + 12,       text).attr(self.node_font);    } else {self.curText.attr('text', text);    }},
Raphael的hover事件有个特性,就是this就是最初调用hover函数注册时间的那个对象。这点还是比较方便的。