简体   繁体   English

使用 javascript 而不是 ZD223E1439188E478349D52476506C2 在滚动时触发 CSS Animation

[英]Trigger a CSS Animation on scroll using javascript and not jquery

How do I trigger the CSS animation for the svg paths using javascript?如何使用 ZDE9B9ED78D7E2E1DCEEFFEE780E2F91 触发 svg 路径的 CSS animation Tried a lot of ways.尝试了很多方法。 Here i have added a text which is basically a svg image.在这里,我添加了一个基本上是 svg 图像的文本。 The animation works correctly and all i want is that the animation triggers only when scrolled. animation 工作正常,我想要的是 animation 仅在滚动时触发。

 * { background-color: black; }.banner { color: white; font-size: 30px; width: 100%; height: 800px; } #logo { position: absolute; left: 50%; transform: translateX(-50%); } #logo path:nth-child(1) { stroke-dasharray: 545px; stroke-dashoffset: 545px; animation: line-anim 2s ease forwards; } #logo path:nth-child(2) { stroke-dasharray: 303px; stroke-dashoffset: 303px; animation: line-anim 2s ease forwards.3s; } #logo path:nth-child(3) { stroke-dasharray: 439px; stroke-dashoffset: 439px; animation: line-anim 2s ease forwards.6s; } #logo path:nth-child(4) { stroke-dasharray: 526px; stroke-dashoffset: 526px; animation: line-anim 2s ease forwards.9s; } #logo path:nth-child(5) { stroke-dasharray: 525px; stroke-dashoffset: 525px; animation: line-anim 2s ease forwards 1.2s; } @keyframes line-anim { to { stroke-dashoffset: 0; } }

Move all animation css to a separate class except the animation-delay.将所有 animation css 移动到单独的 class 中,动画延迟除外。 Add Scroll listener and add this class to the logo.添加滚动侦听器并将此 class 添加到徽标中。

 document.addEventListener('scroll', function() { document.querySelector('.logo').classList.add('logo-animation') })
 .logo-animation { animation:line-anim 2s ease forwards } #logo:path { animation-delay: 2s; }

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

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