<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          React實現(xiàn)打字機效果~

          共 3991字,需瀏覽 8分鐘

           ·

          2023-08-24 04:25

          戳上方“執(zhí)行上下文”,選擇“置頂公眾號

          關(guān)鍵時刻,第一時間送達!


          項目中有一個地方用到打字機的效果,并且在文字顯示完成之后可以選擇是否自動跳轉(zhuǎn)到下一步驟。另外這個打字機效果在多個頁面模塊中使用,區(qū)別是打字完成后是否跳轉(zhuǎn)。

          核心代碼

          useEffect(() => {
              let currentIndex = 0;
              const interval = setInterval(() => {
                if (currentIndex < originalText.length - 1) {
                  setText((prevText) => prevText + originalText[currentIndex]);
                  currentIndex++;
                } else {
                  clearInterval(interval);
                  if (destination && destination.trim() !== '') {
                    timerRef.current = window.setTimeout(() => {
                      navigate(destination);
                    }, 1000); // 在1秒后跳轉(zhuǎn)到目標頁面
                  }
                }
              }, 100);
              
              return () => {
                clearInterval(interval);
                if (timerRef.current) {
                  clearTimeout(timerRef.current);
                }
              };
          }, [originalText, destination, navigate]);
          在useEffect中,用了一個定時器來逐個字符地將原始文本添加到當前文本中。每100毫秒添加一個字符,直到添加完整個原始文本。然后,我們清除定時器以停止動畫。另外引入了useHistory鉤子來獲取路由的history對象。在useEffect中,當打字機效果完成后,用setTimeout函數(shù)來延遲1秒后執(zhí)行跳轉(zhuǎn)操作。

          如何使用

          import React from 'react';
          import Typewriter from './Typewriter';

          const App: React.FC = () => {
            return (
              <div>
                <Typewriter originalText="Hello, World!" destination="/other-page" />
              </div>
            );
          };

          export default App;

          其中 originalText 是需要打印的文本,destination 是文字打印完后需要跳轉(zhuǎn)的頁面。如果打印后停留當前頁面則無需該參數(shù)。

          實際效果


          完整代碼

          import React, { useState, useEffect, useRef } from 'react';
          import { useNavigate } from 'react-router-dom';

          interface TypewriterProps {
            originalText: string;
            destination?: string;
          }
          const Typewriter: React.FC<TypewriterProps> = ({ originalText, destination }) => {
              const [text, setText] = useState('');
              const navigate = useNavigate();
              const timerRef = useRef<number | null>(null);

              ## 核心代碼
              
              return (
                  <div className="typewriter">
                    <h1>{text}</h1>
                  </div>
              );
          };

          export default Typewriter;

          圖片分享

          前端公眾號和交流群



          瀏覽 334
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产高清无码在线视频 | 九九九九久久久久 | 国产欧美视频在线 | 无码天堂 | 人人要人人射 |