Hovering over a set of elements in Raphaeljs

后端 未结 4 1435
囚心锁ツ
囚心锁ツ 2021-01-03 06:26

I have a set that only contains a rectangle.

var hoverTrigger = this.paper.set();
var outline = this.paper.rect();
outline.attr({
...
hoverTrigger.push(outli         


        
4条回答
  •  余生分开走
    2021-01-03 07:17

    Having faced this limitation myself recently, I decided to write a small extension to Raphael called hoverInBounds that resolves it.

    Simply, once the mouse enters the element we keep track of when it actually moves outside its bounds - executing the hover out function then, not before.

    Demo: http://jsfiddle.net/amustill/Bh276/1

    Raphael.el.hoverInBounds = function(inFunc, outFunc) {
        var inBounds = false;
    
        // Mouseover function. Only execute if `inBounds` is false.
        this.mouseover(function() {
            if (!inBounds) {
                inBounds = true;
                inFunc.call(this);
            }
        });
    
        // Mouseout function
        this.mouseout(function(e) {
            var x = e.offsetX || e.clientX,
                y = e.offsetY || e.clientY;
    
            // Return `false` if we're still inside the element's bounds
            if (this.isPointInside(x, y)) return false;
    
            inBounds = false;
            outFunc.call(this);
        });
    
        return this;
    }
    

    Place the above block of code before you create your Raphael paper object.

提交回复
热议问题