Apply Hover event in Raphael on a set of paths

瘦欲@ 提交于 2019-12-03 08:47:36

This is the age old problem where the event you're using to highlight isn't referring to the object you think it is. Specifically the this reference.

It's midnight, I'm tired and I've messed with your code. Here's what I did

Create an object to wrap your set of paths, and setup the mouseover event to refer to the objects set. The magic here is that by using a reference to an object variable, the event will be locked to it and everything works.

So. Heres the object. I put it at the top of mapclasses.js

function setObj(country,myset)
{
    var that = this;
    that.country = country;
    that.myset = R.set();

    that.setupMouseover = function()
    {
        that.myset.mouseover(function(event){
            myvar = that;   
            // in the mouseover, we're referring to a object member that.myset
            // the value is locked into the anonymous object
            that.myset.attr({"fill":"#ffaa00"});
        });
    }

    that.addPath = function(newpath)
    {
       that.myset.push(newpath); 
    }

    return that;
}

Then in the function drawSets (line 80)

drawSets: function(){
    for (country in this.setsArr){
        var setset= R.set();
                    var holderObj = null;
                    //
                    // Create an object to hold all my paths
                    //
                    if (country == 'canada')
                    {
                       holderObj = setObj (country, setset);
                    }
        var zone = this.setsArr[country];
        for (group in zone){
            var path = R.path(this.setsArr[country][group].path);

            setset.push(path);
                            if (country == 'canada')
                            {
                               // add the path to the holder obj
                               holderObj.addPath(path);
                            }
        }

                    if (country == 'canada')
                    {
                       // once all paths for the object are loaded, create a mouseover
                       // event
                       holderObj.setupMouseover();
                    }

        var attri = this.options.attributes;
        setset.attr(attri);
        var x = this.setsArr[country].translate.x;
        var y = this.setsArr[country].translate.y;
        setset.translate(x,y);



    }   
}

Note: I've done this for Canada only. Also, I've only applied the mouseover. It should be trivial for you to apply the associated mouseout. Also you'll need an object for each country, which you'll probably want to store.

Sorry this isn't clearer. As I said, it's late. If you want, I can send you the modified js file, or stick it onto dropbox, but that probably goes against the spirit of StackOverflow.

If you have any problems with this, ask away and I'll try to help.

Best of luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!