将鼠标悬停在 Raphaeljs 中的一组元素上

2022-01-22 00:00:00 hover javascript raphael

我有一个只包含一个矩形的集合.

I have a set that only contains a rectangle.

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

鼠标悬停时,矩形应该会扩大,并添加一些链接,鼠标关闭后,链接会消失,矩形又变小了.

Upon hover, the rectangle is supposed to expand, and some links are added to it, and upon mouse off, the links disappear and the rectangle becomes small again.

hoverTrigger.hover(function () {
  var link = this.paper.text();
  hoverTrigger.push(link);
  outline.animate({
  ...
}, function() {
  link.remove();
  outline.animate({
  ...
});

但是,悬停功能似乎是单独应用于集合中的每个项目,而不是整个集合,因为当您将鼠标悬停在链接上时,悬停功能会触发并且链接消失.有时,该框会快速连续地悬停在事件上和悬停在事件上,并发出声响.

However, it seems like the hover function is being applied to each item in the set individually, and not the whole set, because when you go to mouse over a link, the hover off function fires and the link disappears. Sometimes the box gets hover on and hover off events in quick succession and spazzes.

有没有一种方法可以将悬停应用到一组事物上,以便在该组中的两个事物之间进行鼠标悬停不会触发悬停?

Is there a way to apply a hover to a set of things, so that mousing between two things in the set doesn't trigger hover off?

推荐答案

最近我自己也遇到了这个限制,我决定为 Raphael 编写一个名为 hoverInBounds 的小扩展来解决它.

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.

演示: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;
}

在创建 Raphael 纸对象之前放置上述代码块.

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

相关文章