简体   繁体   English

GSAP 和 IntersectionObserver:文本 .from opacity:0 to 1 在动画前闪烁

[英]GSAP and IntersectionObserver: Text .from opacity:0 to 1 flashing before animation

I am using GSAP and IntersectionObserver to animate every character of every h1 on scroll.我正在使用 GSAP 和 IntersectionObserver 为滚动中每个h1每个字符设置动画。

Everything seems to be working but the opacity part of the animation doesn't work as expected.一切似乎都在工作,但动画的不opacity部分没有按预期工作。 Basically one can see the h1 before it goes to opacity:0 and then back to 1 (it reminds me of the infamous Flash Of Unstyled Text).基本上人们可以在h1变为opacity:0之前看到它,然后又回到 1(这让我想起了臭名昭著的 Flash Of Unstyled Text)。 I am using the .from method.我正在使用.from方法。 I would like every h1 to be invisible before the animation but I can't figure out what I am doing wrong.我希望每个h1在动画之前都是不可见的,但我不知道我做错了什么。 Please check the snippet.请检查代码段。

 const titles = document.querySelectorAll("h1"); const options = { root: null, threshold: 0.25, rootMargin: "-200px" }; const observer = new IntersectionObserver(function(entries, observer) { entries.forEach(entry => { if (!entry.isIntersecting) { return; } entry.target.classList.add("anim-text"); // TEXT SPLITTING const animTexts = document.querySelectorAll(".anim-text"); animTexts.forEach(text => { const strText = text.textContent; const splitText = strText.split(""); text.textContent = ""; splitText.forEach(item => { text.innerHTML += "<span>" + item + "</span>"; }); }); // END TEXT SPLITTING // TITLE ANIMATION const charTl = gsap.timeline(); charTl.set("h1", { opacity: 1 }).from( ".anim-text span", { opacity: 0, x: 40, stagger: { amount: 1 } }, "+=0.5" ); observer.unobserve(entry.target); // END TITLE ANIMATION }); }, options); titles.forEach(title => { observer.observe(title); });
 * { color: white; padding: 0; margin: 0; } .top { display: flex; justify-content: center; align-items: center; font-size: 2rem; height: 100vh; width: 100%; background-color: #279AF1; } h1 { opacity: 0; font-size: 4rem; } section { padding: 2em; height: 100vh; } .sec-1 { background-color: #EA526F; } .sec-2 { background-color: #23B5D3; } .sec-3 { background-color: #F9C80E; } .sec-4 { background-color: #662E9B; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.5/gsap.min.js"></script> <div class="top">Scroll Down</div> <section class="sec-1"> <h1>FIRST</h1> </section> <section class="sec-2"> <h1>SECOND</h1> </section> <section class="sec-3"> <h1>THIRD</h1> </section> <section class="sec-4"> <h1>FOURTH</h1> </section>

Thanks a lot in advance for your help!非常感谢您的帮助!

This is indeed a flash of unstyled content (FOUC) that occurs because the JavaScript waits to run until the page has loaded.这确实是一闪而过的无样式内容 (FOUC),因为 JavaScript 会等到页面加载后才运行。 GreenSock actually has a tutorial on removing FOUC that I recommend. GreenSock 实际上有一个我推荐的关于去除 FOUC 的教程

The basic approach is to hide the elements using your CSS and modify your JS to work with the changed CSS (such as changing the .from() to a .to() or .fromTo()).基本方法是使用 CSS 隐藏元素并修改 JS 以使用更改后的 CSS(例如将 .from() 更改为 .to() 或 .fromTo())。 You could do that by adding h1 { opacity: 0 } to your CSS and then add the following to the JS: gsap.set(h1, {opacity: 1});您可以通过将h1 { opacity: 0 }添加到您的 CSS,然后将以下内容添加到 JS 中: gsap.set(h1, {opacity: 1}); . .

Side note: GSAP has its own SplitText plugin that makes it easy to customize how the text is split (including by line), handles non-standard characters, and adds the ability to easily revert to the default.旁注:GSAP 有自己的SplitText 插件,可以轻松自定义文本的拆分方式(包括按行),处理非标准字符,并添加了轻松恢复为默认值的功能。 I highly recommend it if you're going to be splitting text up!如果您要拆分文本,我强烈推荐它!

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

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