Detecting Mouse Events on Multiple Overlapping SVG Elements

后端 未结 1 1366
无人共我
无人共我 2020-12-31 21:42

I\'m trying to detect mousemove events on partially overlapping SVG elements, as in this image

\"enter

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 22:14

    The great comments here gave me the answer: It's possible to propagate the event to underlying elements manually by finding them using getIntersectionList() at the cursor positon.

    $('svg').on('mousemove', function(evt)
    {
        var root = $('svg')[0];
        var rpos = root.createSVGRect();
        rpos.x = evt.clientX;
        rpos.y = evt.clientY;
        rpos.width = rpos.height = 1;
        var list = root.getIntersectionList(rpos, null);
    
        for(var i = 0; i < list.length; i++)
        {
            if(list[i] != evt.target)
            {
                $(list[i]).mousemove();
            }
        }
    });
    

    Working example: http://jsfiddle.net/michaschwab/w0wufbtn/6/

    If the other listeners need the original event object, check out http://jsfiddle.net/michaschwab/w0wufbtn/13/.

    Thanks a lot!!

    0 讨论(0)
提交回复
热议问题