【问题标题】:Adding tween with dynamic variables to Scrollmagic/GSAP timeline将带有动态变量的补间添加到 Scrollmagic/GSAP 时间线
【发布时间】:2016-02-09 05:53:04
【问题描述】:

我是一个 Jquery 新手,本来以为我的问题的解决方案很简单,但是我已经尝试了此代码的一百万种排列,但我无法解决它。

我正在使用 Scrollmagic 和 GSAP,并希望从同一点触发三个补间,以便它们一个接一个地触发(略有重叠)。场景有duration: 0,因此用户只启动动画,但不控制其进度。每个补间包含一个 css 转换,用于处理同一个对象 (#boxes3d)。

我想要的效果是:
1) 盒子一开始是平的,但是通过将 css perspective 值更改为 850px 来分解成 3D。
2) 通过更改 css transform 值,框旋转 180 度。
3) 通过恢复为perspective: 50000 再次折叠框。
[ 4) 当用户向后滚动超过触发器时,动画会反转。]

复杂之处在于transform 值还必须包含动态scale,以使#boxes3d 内的绝对定位的div 适合任何屏幕尺寸的框架. scale 值(以及整个 transform)由另一个 Jquery 函数动态设置:scaleDiv(),其中包含嵌套的 rotate3D()reverse3D() 函数。

我想我需要创建一个 timelineMax(或 Lite)来对补间进行排队,但我在将动态 CSS 值放入时间轴时遇到了问题。

我创建了一个 jsfiddle,其中包含我想要串在一起的补间/过渡,以及必须合并到第二个补间中的 scaleDiv 函数。目前他们有不同的场景,但我想把它们都放在一个场景/时间线上。

这是小提琴:http://jsfiddle.net/gbinning/vccxufou/13/。我真的很感激一些指导!非常感谢!

编辑:我还没有正确设置scaleDiv 函数,您需要稍微调整窗口大小才能触发它。

【问题讨论】:

  • 看看 this。它还没有考虑到window.resize,但我想知道我是否朝着正确的方向前进。如果您希望我继续,请告诉我。
  • 现在看,会尽快反馈。谢谢塔希尔
  • .. 它也不适应您拥有的 rotate3Dreverse3DscaleDiv 方法。我仍在试图弄清楚您到底想达到什么目的,以便为您提供另一种方法。
  • 我想当我说爆炸时我把你弄糊涂了——我不希望盒子的面分开——只是为了在视角改变时显示盒子的深度。所以我对我发布的小提琴上的转换很满意,但我遇到的问题是在补间中包含动态值——即translateX(' + parentWidth + 'px) scaleY(' + scale + ') scaleX(' + scale + ')。然后我想使用时间线将三个场景连接在一起,因此只有一个触发器和 0px 的持续时间(但应该运行几秒钟)。 在下一条评论中继续...
  • 你的代码看起来比我的更有希望——我是否认为如果你用TweenMax.set设置css值它会更快地工作?为了看到rotate3D / reverse3D 正常工作,您必须在滚动超过第二个触发器之前稍微调整窗口大小 - 尽管该函数实际上应该与window.onloadscaleDiv 函数同时调用 - 它只是我笨拙的编码。

标签: jquery tween gsap scrollmagic dynamic-css


【解决方案1】:

*如果您不想看到我的故事,请转到标有粗体字的底部以获取简短答案。 发这个问题的人可能早就忘记了这个问题,但是对于任何人来这里,这就是我的解决方案。我将在我的回答中使用 gsap3,因为这是当前的。当我遇到这个问题时,我正在制作一个基于滚动的动画。正如您在代码中看到的那样,我想在我的动画中做的是基于滚动的 div 上的条目动画。问题是下面的CSS

transform: translate(314.18px, 244.9px) rotate(295deg) 

应该有不同的 x 和 y 坐标,具体取决于屏幕尺寸。例如,如果用户从全屏开始,然后调整窗口大小会扭曲动画,所以我想在窗口调整大小时更新坐标。为了解决这个问题,我将 x 和 y 坐标设置为不同函数 width() 和 height() 的返回值,希望 gsap.to 函数将函数作为其值存储在键值对中,但正如在上面的答案不仅存储了一次变量值,而且事实证明它还存储了函数的返回值而不是函数本身,所以我尝试了其他方法。也就是把所有的动画放在一个函数中,每次调整窗口大小时调用。之前和之后的代码如下所示。

解决之前

var vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
function updateDimention() {
    vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
    vh = Math.max(document.documentElement.clienthHeight || 0, window.innerHeight || 0)
}
window.onresize=updateDimention;
let height = function(){return vh};
let width = function(){return vw};

gsap.registerPlugin(ScrollTrigger, MotionPathPlugin);
const entryAnim = gsap.timeline(
        {
            scrollTrigger:{
                scrub:true,
                pin:true,
                trigger:".main",
                start:"top top",
                end:"+=6000px",
            },
            // yoyo:true,
        }
        );
        entryAnim.pause()
        entryAnim.to(
            ".pic1",{
            motionPath:{
                path:[{x:0,y:0,},{x:0.155*width(),y:0.2*height()},{x:0.18*width(),y:0.25*height()},{x:0.23*width(),y:0.395*height()}],
                // type:"cubic",
                autorotate:true,
            }, 
            
        }).to(
            ".pic2",{
            motionPath:{
                path:[{x:0.18*width(),y:0,},{x:0.15*width(),y:0.21*height()},{x:0*width(),y:0.40*height()},{x:-0.000503*width(),y:0.75*height()}],
                // type:"cubic",
                autorotate:true,
            }, 
        
        }).to(
            ".pic3",{
            motionPath:{
                path:[{x:-0.15*width(),y:-0.10,},{x:-0.10*width(),y:-0.5*height()},{x:0*width(),y:0*height()},{x:0.4245*width(),y:0.324*height()}],
                // type:"cubic",
                autorotate:false,
            }, 
        
        })
        
        
        const rotationAnim = gsap.timeline(
        {
            scrollTrigger:{
                trigger:".main",
                start:"top top",
                end:"+=6000px",
                pin:true,
                scrub:true
            },
            // yoyo:true,
        }
        );
        rotationAnim.pause()
        rotationAnim.to(".pic1",{
            rotation:295,
        }).to(
            ".pic2",{
                rotation:329,
            }
        ).to(".pic3",{
            rotation:48,
        })

解决后

var vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
function updateDimention() {
    vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
    vh = Math.max(document.documentElement.clienthHeight || 0, window.innerHeight || 0)
}
let height = function(){return vh};
let width = function(){return vw};

gsap.registerPlugin(ScrollTrigger, MotionPathPlugin);
function Animation(){
    const entryAnim = gsap.timeline(
        {
            scrollTrigger:{
                scrub:true,
                pin:true,
                trigger:".main",
                start:"top top",
                end:"+=6000px",
            },
            // yoyo:true,
        }
        );
        entryAnim.pause()
        entryAnim.to(
            ".pic1",{
            motionPath:{
                path:[{x:0,y:0,},{x:0.155*width(),y:0.2*height()},{x:0.18*width(),y:0.25*height()},{x:0.23*width(),y:0.395*height()}],
                // type:"cubic",
                autorotate:true,
            }, 
            
        }).to(
            ".pic2",{
            motionPath:{
                path:[{x:0.18*width(),y:0,},{x:0.15*width(),y:0.21*height()},{x:0*width(),y:0.40*height()},{x:-0.000503*width(),y:0.75*height()}],
                // type:"cubic",
                autorotate:true,
            }, 
        
        }).to(
            ".pic3",{
            motionPath:{
                path:[{x:-0.15*width(),y:-0.10,},{x:-0.10*width(),y:-0.5*height()},{x:0*width(),y:0*height()},{x:0.4245*width(),y:0.324*height()}],
                // type:"cubic",
                autorotate:false,
            }, 
        
        })
        
        
        const rotationAnim = gsap.timeline(
        {
            scrollTrigger:{
                trigger:".main",
                start:"top top",
                end:"+=6000px",
                pin:true,
                scrub:true
            },
            // yoyo:true,
        }
        );
        rotationAnim.pause()
        rotationAnim.to(".pic1",{
            rotation:295,
        }).to(
            ".pic2",{
                rotation:329,
            }
        ).to(".pic3",{
            rotation:48,
        })
}

Animation()
function updateAnimation (){
    updateDimention();
    Animation();

}

window.onresize=updateAnimation;

简而言之

所以作为一个结论,你想要做的是以下,

function Animation(){
        //in here put all your gsap related animations
    }
    //then down here call the Animation function every time values are changed so that
    //the gap animation can be redefined. In this case I am going to do it in window resize.
    window.onresize = Animation;

【讨论】:

    【解决方案2】:

    所以您希望它动态地旋转、变换和缩放?我在我的项目中做同样的事情..这就是我的做法。也许对你有帮助。

    http://jsfiddle.net/xko7gjty/1/

    $("#abox").addClass("rotate");
    
    var dynamicx = Math.random()*300;
    var dynamicy = Math.random()*300;
    var dynamicscale = Math.random();
    var tween = new TweenMax("#abox", 1, {
                top: dynamicx+"px",
                left:dynamicy+"px",
                scale:dynamicscale,
                //ease: Linear.easeNone,
                delay:.2
            }); 
    

    仅供参考,您必须每次都创建一个新的补间。 gsap 的工作方式是,当您创建补间时,它使用当时的 CSS 值如果您更改了 css,原始补间将只执行原始位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多