【问题标题】:Raphael Animation with scroll event带有滚动事件的拉斐尔动画
【发布时间】:2014-07-01 02:22:05
【问题描述】:

有没有办法使用 Raphaël—JavaScript 库在用户滚动网站时开始为对象设置动画,并在用户停止滚动时停止动画?

我已经创建了动画,但是我找不到任何关于如何将动画与网站滚动条同步的信息。

编辑:

var path6 = paper.path('M 148 140 L 176 164 L 94 189 L 148 140 Z').attr("fill","#9B5024").attr("stroke","transparent").attr("stroke-width",0);

_path3 = Raphael.transformPath('M 148 140 L 102 155 L 94 189 L 148 140 Z');
path6.animate({path: _path3}, 4000);

以上是我的代码,我想做的是将我的动画与页面滚动同步,因此我希望对象在用户滚动时动画化,而不是在 .animate() 中提供 4000 毫秒。

【问题讨论】:

    标签: javascript raphael


    【解决方案1】:

    您可以使用纯 javascript 轻松做到这一点。

    只需应用 Raphaël 启动和停止方法。

    Have a look at this weave.

    var timer = null;
    function scrolling() {
        document.getElementById("Status").innerHTML = "scrolling..";
          if(timer !== null) {
            clearTimeout(timer);        
        }
        timer = setTimeout(function() {
            document.getElementById("Status").innerHTML = "stopped scrolling";
        }, 150);
    }
    
    window.onscroll = scrolling;
    

    更新

    Here's a weave with a stopping and starting animation.

    var paper = new Raphael('Animation', 100, 100);
    var rect = paper.rect(20, 20, 20, 20).attr({fill: '#F00'});
    var anim = Raphael.animation({transform: "r360"}, 2500).repeat(Infinity);
    
    var timer = null;
    function scrolling() {
        document.getElementById("Status").innerHTML = "scrolling..";
          if(timer !== null) {
            rect.animate(anim);      
        }
        timer = setTimeout(function() {
            rect.stop();
        }, 150);
    }
    
    window.onscroll = scrolling;
    

    【讨论】:

    • 我实际上发现了一个小错误 - 滚动的次数越多,动画就越慢。猜猜我以错误的方式开始/停止动画:-/
    • 是的,我也注意到了,我检查了文档是停止动画的正确方法,我尝试删除 .repeat("Infinity") 但同样的结果有什么想法吗?
    • 恐怕 Raphaël 可能不支持这种功能。我尝试了很多组合,但没有一个是正确的。这是我的游乐场 - liveweave.com/cqI2gN - 也许你可以在没有 Raphaël 的情况下构建相同的动画:-/
    • 我发现我们可以使用 .pause(animation) 和 .resume(animation) 看看liveweave.com/4Jt5P7 但恢复后动画停止了
    • 对,我也试过了,好像忘记了 .repeat(infinity)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2011-10-20
    相关资源
    最近更新 更多