简体   繁体   English

拉斐尔动画与滚动事件

[英]Raphael Animation with scroll event

Is there a way to start animating an object when the user is scrolling the website and to stop the animation when user stops scrolling, using Raphaël—JavaScript Library? 有没有一种方法可以使用Raphaël-JavaScript库在用户滚动网站时开始为对象设置动画,并在用户停止滚动时停止动画播放?

I already created the animation but i cant find anything on how to sync the animation with the website scrollbar. 我已经创建了动画,但是找不到有关如何将动画与网站滚动条同步的任何信息。

Edit: 编辑:

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);

The above is my code what i would like to do is to sync my animation with the page scroll, so instead of providing the 4000 ms in .animate() i would like the object to animate as long as the user scrolls. 上面是我的代码,我想做的是使我的动画与页面滚动同步,因此,只要用户滚动,我不希望在.animate()中提供4000毫秒,而是希望该对象进行动画处理。

You can easily do this with pure javascript. 您可以使用纯JavaScript轻松完成此操作。

Just apply the Raphaël start and stop method. 只需应用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;

Update 更新

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM