【问题标题】:Animating multiple scenes in a sequence with ScrollMagic使用 ScrollMagic 为序列中的多个场景设置动画
【发布时间】:2015-02-03 14:30:47
【问题描述】:

我正在尝试使用 ScrollMagic 和 GSAP 重建此介绍效果:http://airnauts.com 请注意滚动时介绍的“幻灯片”(带有文本)如何显示和消失。

基本上,我已经为动画设置了一个舞台控制器、一个场景(包含 div - '.hero-unit')和一些补间。但是,我就是不知道如何按这样的顺序为每张幻灯片(总共三张)制作动画:

  1. 您进入网站并开始滚动。
  2. 第一张幻灯片是动画的(使用 staggerFromTo 方法)。
  3. 当幻灯片完全动画化后,将其不透明度降低回 0(或将其移出屏幕或其他方式)。
  4. 显示第二张幻灯片,如 2 所示。
  5. 与 3 相同。 等等。

我尝试了所有在互联网上找到的解决方案。我尝试使用“TimelineMax”,尝试在使用 onComplete 为幻灯片设置动画后隐藏幻灯片,但似乎没有任何效果。这是我到目前为止的代码:

var pinOrigin = {
                opacity: 0
            };
            var pinEnd = {
                opacity: 1,
                yoyo: true,
                ease: Back.easeOut
            }

            var tl = TweenMax.staggerFromTo('.hero-unit .scene:first-of-type', 1, {opacity:0}, {opacity: 1});

            var pin = new ScrollScene({
                triggerElement: '.hero-unit',
                triggerHook: 'onLeave',
                duration: 1000
            }).setPin('.hero-unit').setTween(tl).addTo(controller);

回顾一下:如何在滚动时设法呈现不同的场景并在它们之间通过漂亮的过渡进行切换??

【问题讨论】:

  • 好的,所以我自己想通了。您必须使用 TimelineMax(或我猜是 Lite)并相对设置不同的场景移动,使用延迟如下:

标签: javascript gsap scrollmagic


【解决方案1】:

your answer的改进我想补充一点,您可以将补间连续添加到时间轴,它们会自动添加到时间轴的末尾。

因此,您的动画代码的更好方法是:

var tl = new TimelineMax()
    .add(TweenMax.to('.hero-unit .scene:first-of-type', 1, {opacity: 1}))
    .add(TweenMax.to('.hero-unit .scene:first-of-type', 1, {opacity: 0}))
    .add(TweenMax.to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:1}))
    .add(TweenMax.to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:0}))
    .add(TweenMax.to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:1}))
    .add(TweenMax.to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:0}));

如果您想随时添加任何内容,这样代码就更易于管理。 更多信息请查看http://greensock.com/docs/#/HTML5/GSAP/TimelineMax/add/

如果您只想在时间线的末尾添加一个 .to 补间,您可以使用 TimelineMax.to() method 使其更加简洁。
来自 GSAP 文档:

将 TweenLite.to() 补间添加到时间线的末尾(或在其他地方使用“位置”参数) - 这是一种与 add(TweenLite.to(...) ) 但代码更少。

因此,这将使这成为最适合您的 GSAP 动画代码:

var tl = new TimelineMax()
    .to('.hero-unit .scene:first-of-type', 1, {opacity: 1})
    .to('.hero-unit .scene:first-of-type', 1, {opacity: 0})
    .to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:1})
    .to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:0})
    .to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:1})
    .to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:0});

【讨论】:

    【解决方案2】:

    好的,所以我自己想通了。您必须使用 TimelineMax(或我猜是 Lite)并相对设置不同的场景移动,使用延迟如下:a

    var tl = new TimelineMax().add([
                        TweenMax.to('.hero-unit .scene:first-of-type', 1, {opacity: 1}),
                        TweenMax.to('.hero-unit .scene:first-of-type', 1, {opacity: 0, delay: 1}),
                        TweenMax.to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:1, delay: 2}),
                        TweenMax.to('.hero-unit .scene:nth-of-type(2)', 1, {opacity:0, delay: 3}),
                        TweenMax.to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:1, delay: 4}),
                        TweenMax.to('.hero-unit .scene:nth-of-type(3)', 1, {opacity:0, delay: 5})
                    ]);
    
                var pin = new ScrollScene({
                    triggerElement: '.hero-unit',
                    triggerHook: 0,
                    duration: 2000
                }).setPin('.hero-unit').setTween(tl).addTo(controller);
    

    【讨论】:

    • 使用这样的延迟实际上违背了使用时间轴的目的。 Timeline 的想法是让补间动画按顺序自动制作,而不必延迟每个补间等待另一个补间。因此,当您调整补间的持续时间时,您不需要更改所有后续的延迟。无论如何,简的做法是正确的。
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 2023-03-30
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多