Raphael JS - 鼠标悬停时开始/继续动画。鼠标移开时暂停动画

发布于 2024-12-22 05:19:01 字数 798 浏览 2 评论 0原文

使用Raphael JS,有没有一种方法可以在鼠标悬停期间使圆圈向右(或任何方向)移动,然后当光标不再位于圆圈上时暂停/停止移动。

我尝试了几种不同的方法,但它们都有错误。主要问题之一是:如果鼠标光标进入圆圈后没有移动,那么一旦圆圈移动到鼠标光标不再位于圆圈顶部的位置,就不会触发“mouseout”。

您将在这些不同的尝试中明白我的意思:

1)带有暂停/恢复的动画:

jsfiddle.net/fKKNt/

  • 但是动画是不稳定且不可靠的。如果将鼠标悬停在对象上,当对象移出鼠标光标所在位置时,它不会触发“鼠标移开”侦听器。

2)通过鼠标悬停和重新定位.attr("cx"):

jsfiddle.net/c4BFt/

  • 但我们希望动画在光标位于圆圈中时继续。

3)使用setInterval(如建议: “if mouseover”或“do while” JavaScript/jQuery 中的 mouseover"):

jsfiddle.net/9bBcm/

  • 但是,当圆圈移动到光标所在位置之外时,不会调用“mouseout”。即圆圈移动到应该调用“mouseout”的位置,但它没有被调用。 “悬停”也会发生同样的事情:

jsfiddle.net/STruB/

Using Raphael JS, is there a way to make a circle move to the right (or any direction) during mouseover, and then pause/stop the movement when the cursor is no longer on the circle.

I've tried few different methods, but they have bugs. One of the main issues is: if the mouse cursor doesn't move after entering the circle, "mouseout" will not be triggered once the circle moves to a location where the mouse cursor is no longer over top of the circle.

You'll see what I mean in these different attempts:

1) Animate with pause / resume:

jsfiddle.net/fKKNt/

  • But the animation is jerky and unreliable. If you hover over the object, as the object moves outside of where the mouse cursor is, it doesn't trigger the "mouseout" listener.

2) Repositioning with mouseover & .attr("cx"):

jsfiddle.net/c4BFt/

  • But we want the animation to continue while the cursor is in the circle too.

3) Using setInterval (as suggested in:
An "if mouseover" or a "do while mouseover" in JavaScript/jQuery):

jsfiddle.net/9bBcm/

  • But "mouseout" is not called as the circle moves outside of where the cursor lies. I.e. the circle move to a location where "mouseout" should be called, but it is not called.
    The same thing happens with "hover":

jsfiddle.net/STruB/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-12-29 05:19:01

我确信有一种更优雅的方法可以做到这一点,但在我的脑海中,你可以尝试这样的事情: http://jsfiddle.net/D6Ps4/2/

如果由于某种原因消失,我添加了下面的代码。该解决方案只需启动动画,然后检查鼠标光标(注意 e.offsetX/e.offsetY)是否位于 Raphael 对象的边界框内(Element.getBBox()) 以某个设定的时间间隔。如果是,则不执行任何操作,并使用 setTimeout 再次检查一段时间,如果不是,则暂停动画。

    var paper = Raphael("holder");
    var animObject = Raphael.animation({cx: 400}, 5000);
    circle = paper.circle(90, 90, 45).attr({fill: "#0E4"});
    var timer;

    circle.mouseover(function(e) {
        var anim = function(shouldAnim) {
            if (shouldAnim) {
                circle.animate(animObject);
            }
            if (!mouseInsideCircle(e, circle)) {
                circle.pause();
                return;            
            } else {
                timer = setTimeout(function() {anim(false)}, 20);
            }
        }
        anim(true);
    });

    circle.mouseout(function() {
        this.pause();
        clearTimeout(timer);
    });

    var mouseInsideCircle = function(e, c) {
        var bb = c.getBBox();
        if (e.offsetX > bb.x && e.offsetY > bb.y) {
            return true;
        }
        return false;
    }

我确信该解决方案是有缺陷的(它检查的是boundBox,而不是圆本身;它还假设圆正在向右移动)并且可能并不理想,但它似乎工作得相当顺利,并希望让您走上正确的道路。

I'm sure there's a much more elegant way to do this, but off the top of my head, you could try something like this: http://jsfiddle.net/D6Ps4/2/

In case that disappears for some reason, I've included the code below. The solution simply initiates the animation, then checks to see if the mouse cursor (note the e.offsetX/e.offsetY) is within the bounding box of your Raphael Object (Element.getBBox()) at some set interval. If it is, do nothing and use setTimeout to check again in some time, if it's not, pause the animation.

    var paper = Raphael("holder");
    var animObject = Raphael.animation({cx: 400}, 5000);
    circle = paper.circle(90, 90, 45).attr({fill: "#0E4"});
    var timer;

    circle.mouseover(function(e) {
        var anim = function(shouldAnim) {
            if (shouldAnim) {
                circle.animate(animObject);
            }
            if (!mouseInsideCircle(e, circle)) {
                circle.pause();
                return;            
            } else {
                timer = setTimeout(function() {anim(false)}, 20);
            }
        }
        anim(true);
    });

    circle.mouseout(function() {
        this.pause();
        clearTimeout(timer);
    });

    var mouseInsideCircle = function(e, c) {
        var bb = c.getBBox();
        if (e.offsetX > bb.x && e.offsetY > bb.y) {
            return true;
        }
        return false;
    }

I'm sure the solution is flawed (it's checking the boundBox, not the circle itself; it also assumes the circle is moving right) and perhaps not ideal, but it seems to work reasonably smoothly and hopefully gets you on the right path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文