亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我正在嘗試使用 anime.js 和 jQuery 將動畫添加到我的網頁標題中,但它會引發錯誤

我正在嘗試使用 anime.js 和 jQuery 將動畫添加到我的網頁標題中,但它會引發錯誤

夢里花落0921 2023-03-24 16:14:23
我有一個關于我們的網頁,我需要為這個標題制作動畫:我找到了這個,它使用anime.jsand jQuery。這是我的About.js:import React from 'react';import '../css/About.css'import '../scripts/aboutAnimation'import AboutInfo from "../components/AboutInfo";const About = () => {    return (        <div className="containerAbout">            <div className="title-container">                <p className="title">About us...</p>            </div>            <div className="forest">                <AboutInfo                    name="Vasiliy Pupkin"                    number="+375 29 973 79 43"                    email="[email protected]"                    position="top"                />                <AboutInfo                    name="Aleksey Smirnov"                    number="+375 29 337 91 07"                    email="[email protected]"                    position="middle"                />                <AboutInfo                    name="Ivan Karpovski"                    number="+375 29 655 74 25"                    email="[email protected]"                    position="down"                />            </div>        </div>    );}export default About;這是我的aboutAnimation.js:import anime from 'animejs/lib/anime.es.js';let textWrapper = document.getElementsByClassName('title');textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");anime.timeline({loop: true})    .add({        targets: '.title .letter',        translateX: [40,0],        translateZ: 0,        opacity: [0,1],        easing: "easeOutExpo",        duration: 1200,        delay: (el, i) => 500 + 30 * i    }).add({    targets: '.title .letter',    translateX: [0,-30],    opacity: [1,0],    easing: "easeInExpo",    duration: 1100,    delay: (el, i) => 100 + 30 * i});這是我得到的錯誤:TypeError: Cannot read property 'replace' of undefined我刪除了第 5 行并textWrapper通過console.log()命令檢查是否存在,控制臺顯示如下內容:順便說一句:如果我在 Chrome 控制臺中輸入這兩行,我會得到:
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

你應該使用,


let textWrapper = document.getElementsByClassName('title')[0];

textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");

因為 getElementsByClassName 返回具有所有給定類名的所有子元素的類數組對象。


嘗試為 p 標簽分配一些 id 并嘗試使用 getElementById


let textWrapper = document.getElementById('title');

textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");

正在添加新的功能組件解決方案!我想這可能對你有幫助。


import React from 'react';

import '../css/About.css';

import AboutInfo from "../components/AboutInfo";


// THE PACKAGE I HAVE USED

import anime from 'animejs';


export default function App() {

  const animationRef = React.useRef(null);

  React.useEffect(() => {

    let textWrapper = document.getElementsByClassName('title');

    textWrapper[0].innerHTML = textWrapper[0].textContent.replace(/\S/g, "<span className='letter'>$&</span>");


    animationRef.current =

      anime.timeline({ loop: true })

        .add({

          targets: document.querySelectorAll('.title, .letter'),

          translateX: [40, 0],

          translateZ: 0,

          opacity: [0, 1],

          easing: "easeOutExpo",

          duration: 1200,

          delay: (el, i) => 500 + 30 * i

        }).add({

          targets: document.querySelectorAll('.title, .letter'),

          translateX: [0, -30],

          opacity: [1, 0],

          easing: "easeInExpo",

          duration: 1100,

          delay: (el, i) => 100 + 30 * i

        });

  }, []);

  return (

    <div className="containerAbout">

        <div className="title-container">

            <p className="title">About us...</p>

        </div>

        <div className="forest">

            <AboutInfo

                name="Vasiliy Pupkin"

                number="+375 29 973 79 43"

                email="[email protected]"

                position="top"

            />

            <AboutInfo

                name="Aleksey Smirnov"

                number="+375 29 337 91 07"

                email="[email protected]"

                position="middle"

            />

            <AboutInfo

                name="Ivan Karpovski"

                number="+375 29 655 74 25"

                email="[email protected]"

                position="down"

            />

        </div>

    </div>

);

}


查看完整回答
反對 回復 2023-03-24
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號