[英]Why is GSAP enter opacity animation not working correctly?
I'm using GSAP with barba.js to create smooth page transitions but I have a bug that I can't debug.我正在使用带有barba.js的GSAP来创建平滑的页面转换,但我有一个无法调试的错误。 The transition itself is working as expected, the page fades, a pjax call occurs, the page content is updated and then the new page content fades in.
转换本身按预期工作,页面淡出,发生 pjax 调用,页面内容被更新,然后新的页面内容淡入。
However, once the page has faded back in, if I repeat the transition by going to another page, the fade-in opacity gets stuck before reaching it's final value of '1'.但是,一旦页面淡入,如果我通过转到另一个页面来重复转换,淡入不透明度会在达到其最终值“1”之前卡住。 I have tested this across pages and the first transition is always correct, regardless of which enter/leave pages are being used.
我已经跨页面测试了这一点,无论使用哪个进入/离开页面,第一个转换总是正确的。
My question is why?我的问题是为什么? Here is my barba.js using GSAP:
这是我使用 GSAP 的 barba.js:
barba.init({
transitions: [{
name: 'test-transition',
leave(data) {
return gsap.to(data.current.container, {
opacity: 0 // works correctly
});
},
afterLeave() {
reinitModal(); // works correctly
},
beforeEnter: ({ next }) => {
window.scrollTo(0, 0); // works correctly
reinitCounter(); // works correctly
reinitScripts(); // works correctly, note this doesn't reinitialise this jS file
},
enter(data) {
return gsap.from(data.next.container, {
opacity: 0 // problem child, only on cycles after the first one
});
}
}]
});
See below the image of console showing the opacity stopping on a random percentage on second page load.请参阅下面的控制台图像,显示在第二页加载时随机百分比停止不透明度。
Has anyone come across this before, is there a known solution?有没有人遇到过这种情况,有已知的解决方案吗?
Despite asking around on several forums and several more hours of debugging I have been unable to get to the bottom of this, so I have created a little workaround solution which produces no noticeable performance lag etc.尽管在几个论坛上进行了询问并进行了几个小时的调试,但我无法深入了解这一点,因此我创建了一个小变通解决方案,它不会产生明显的性能延迟等。
Instead of using gsap.from
we use gsap.to
in combination with a separate jS function to reset the opacity to 0 in the middle of the transition.我们不使用
gsap.from
,而是将 gsap.to 与单独的 jS gsap.to
结合使用,以在过渡中间将不透明度重置为 0。 Below is the workaround transition:以下是解决方法转换:
barba.init({
// debug: true,
// logLevel: 'error',
transitions: [{
name: 'test-transition',
leave(data) {
reinitProjectDetails();
return gsap.to(data.current.container, {
opacity: 0
});
},
afterLeave() {
reinitModal();
},
beforeEnter: ({ next }) => {
window.scrollTo(0, 0);
reinitTitleScene();
reinitScripts();
reinitOpacity(); // this is calling our external opacity reset script
reinitHeader();
},
enter(data) {
return gsap.to(data.next.container, { // note gsap.to instead of gsap.from
opacity: 1, // animating Opacity value to '1' rather than from '0'
delay: 0.45, // makes for a smoother transition
});
}
}]
});
Outside of the transition script is the opacity reset/reinit function which looks like this:过渡脚本之外是不透明度重置/重新初始化 function,如下所示:
function reinitOpacity() {
$('main').css("opacity", "0"); // note see below
}
Note that the 'main' container is whichever container has the 'barba-container' class.请注意,“主”容器是具有“barba-container”class 的容器。 I hope this helps someone in need.
我希望这可以帮助有需要的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.