简体   繁体   English

Raphael.js中的火灾事件

[英]Fire event in Raphael.js

I have an Raphael element with click event handler: 我有一个带有单击事件处理程序的Raphael元素:

var paper = Raphael("container", 770, 160);
var c = paper.rect(10, 10, 50, 50);
c.click(function () {
    alert("triggering");
})

How I can manually fire this event? 如何手动触发此事件? ( c.click() don't work) c.click()不起作用)
Thanks! 谢谢!

Although this question is already answered, I will post my solution which I found out by random. 尽管已经回答了这个问题,但是我将发布我随机发现的解决方案。 It's possible with the Raphael internals: Raphael内部结构有可能:

When you attach an event listener like element.click(func) then the element object holds an array with all events. 当您附加一个事件监听器(例如element.click(func)element对象将保存一个包含所有事件的数组。 In this array there's an object which has a method f (strange naming convention) which triggers the event. 在此数组中,有一个对象,该对象具有触发事件的方法f (奇怪的命名约定)。

So to sum it up you can call your event with knowing the order of your events, in your case there's just the click event which is on index 0, you can call it like: c.events[0].f(); 因此, c.events[0].f(); ,您可以在知道事件顺序的情况下调用事件,在您的情况下,只有索引为0的click事件,您可以这样调用它: c.events[0].f();

A more generic solution would be a function like 一个更通用的解决方案是像

function triggerClick(element) {
    for(var i = 0, len = element.events.length; i < len; i++) {
        if (element.events[i].name == 'click') {
            element.events[i].f();
        }
    }
}​

But beware that if you had multiple click events all were triggered. 但是请注意,如果您有多个click事件,则会全部触发。

Here's a fiddle to demonstrate. 这里有个小提琴来演示。

EDIT : 编辑:

The solution purposed by Dan Lee is working better. Dan Lee设计的解决方案效果更好。

Even though this has already been answered a while ago, I was looking for something a little "more native" in nature. 即使前一阵子已经回答了这个问题,我也在自然界中寻找一些“更加本土化”的东西。 Here's how I go about it without relying on Mootools or jQuery. 这是我不依赖Mootools或jQuery的解决方法。

var evObj = document.createEvent('MouseEvents'); 
evObj.initEvent('click', true, false); 
c.node.dispatchEvent(evObj); 

This basically creates the event in the browser and then dispatches it to the node associated with the Raphaël object. 这基本上是在浏览器中创建事件,然后将其分发到与Raphaël对象关联的节点。

Here's also the MOZ link for reference: https://developer.mozilla.org/en-US/docs/DOM/document.createEvent 这也是MOZ链接供参考: https : //developer.mozilla.org/en-US/docs/DOM/document.createEvent

I actually found a slightly more elegant way to use Kris' method. 实际上,我发现了一种使用Kris方法的更优雅的方法。 Raphael supports native extension of the element object so I built a little patch to add the trigger method to raphael Raphael支持element对象的本机扩展,因此我构建了一个小补丁以将触发方法添加到Raphael

here it is: 这里是:

//raphael programatic event firing
    Raphael.el.trigger = function (str, scope, params){ //takes the name of the event to fire and the scope to fire it with
        scope = scope || this;
        for(var i = 0; i < this.events.length; i++){
            if(this.events[i].name === str){
                this.events[i].f.call(scope, params);
            }
        }
    };

I set up a Js fiddle to show how it works: here 我设置了一个Js小提琴以演示其工作原理: 这里

I write a plug-in for this, use event to calculate the right position; 我为此编写了一个插件,使用事件来计算正确的位置;

Raphael.el.trigger = function(evtName, event) {
    var el = this[0];      //get pure elements;
    if (event.initMouseEvent) {     // all browsers except IE before version 9
        var mousedownEvent = document.createEvent ("MouseEvent");
        mousedownEvent.initMouseEvent (
            "mousedown", true, true, window, 0, 
            event.screenX, event.screenY, event.clientX, event.clientY, 
            event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 
            0, null);
        el.dispatchEvent (mousedownEvent);
    } else {
        if (document.createEventObject) {   // IE before version 9
            var mousedownEvent = document.createEventObject (window.event);
            mousedownEvent.button = 1;  // left button is down
            el.fireEvent (evtName, mousedownEvent);
        }
    }
};

Usage: 用法:

circle2.mousedown(function(e) {
  var circle = this.clone();
  circle.drag(move, down, up);
  circle.trigger("mousedown", e);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM