CSS实现 文字逐行显示&文字逐个显示
一,文字逐行显示
HTML:
<div className="LetterToFuture__homeTexts"> {Array.from(new Array(5)).map((_, idx) => ( /* eslint-disable-next-line react/no-array-index-key */ <div key={idx} className="LetterToFuture__homeText" /> ))} </div>
CSS:由于字体效果比较特殊,所以每行文字采用的是图片
$fadeInDur: 1500ms; $textWidth: "231px", "124px", "97px", "96px", "213px"; .LetterToFuture__homeTexts { display: flex; flex-direction: column; align-items: center; justify-content: flex-start; } .LetterToFuture__homeText { background: url(../../images/text_1.png) no-repeat center center / 100% 100%; height: 15px; margin-top: 25px; animation: slide-up-in #{$fadeInDur} ease-out both 1/*infinite*/; &:nth-child(1) { margin-top: 15px; } /* 官网的 list.nth 写法 not working - https://stackoverflow.com/questions/53155503/how-to-find-the-scss-index-of-the-element-in-the-list */ /*@debug list.nth([line1, line2, line3], -1);*/ /*@debug list.nth(10px 12px 16px, 2);*/ /*@debug "------------------------------------------------------------------------------------------";*/ /* 正常 */ /*@debug nth($textWidth, 1);*/ @for $idx from 1 through 5 { &:nth-child(#{$idx}) { animation-delay: #{($idx - 1) * $fadeInDur}; background-image: url(../../images/text_#{$idx}.png); width: #{nth($textWidth, $idx)}; } } } @keyframes slide-up-in { 0% { opacity: 0; transform: translate(0, 15px); } 100% { opacity: 1; transform: translate(0, 0); } }
以上实现效果是:每行文字位移由下至上,透明度从0到1,逐渐显示,如果你不想要位移变化,将css里位移相关代码去掉即可。
二,文字逐个显示
HTML:
<div className="WorldBookDayQuestionOne__textWrap"> {text.split('').map((t, idx) => ( /* eslint-disable-next-line react/no-array-index-key */ <span key={idx} className="WorldBookDayQuestionOne__text"> {t} </span> ))} </div>
CSS:
$fadeInDur: 1500ms; .WorldBookDayQuestionOne{ &__textWrap { width: 150px; margin: 0 auto; margin-top: 100px; background-color: burlywood; } &__text { opacity: 0; animation: fade-in #{$fadeInDur} ease-out both 1/*infinite*/; @for $idx from 1 through 50 { &:nth-child(#{$idx}) { animation-delay: #{($idx - 1) * $fadeInDur}; } } } } @keyframes fade-in { 0% { opacity: 0; } 100% { opacity: 1; } }
以上实现效果是:文字透明度由0到1逐个显示,当一行显示不下的时候会自动往下一行折叠,一行显示多少文字取决于父元素容器大小。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库