国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 js教程 輕鬆的 React 動(dòng)畫:Framer 運(yùn)動(dòng)指南

輕鬆的 React 動(dòng)畫:Framer 運(yùn)動(dòng)指南

Jan 23, 2025 pm 10:39 PM

Effortless React Animation: A Guide to Framer Motion

TL;DR: 這篇部落格文章提供了使用 React 動(dòng)畫庫(kù) Framer Motion 的全面指南。它涵蓋了運(yùn)動(dòng)組件、變體和過(guò)渡等關(guān)鍵概念,並提供了創(chuàng)建淡入淡出按鈕、滑入側(cè)邊欄、可拖曳模式和卡片翻轉(zhuǎn)動(dòng)畫的實(shí)際範(fàn)例。

身為前端開發(fā)人員,我們的首要任務(wù)是建立讓使用者參與的 Web 應(yīng)用程式。這可以透過(guò)建立互動(dòng)式頁(yè)面並提供更好的使用者體驗(yàn)來(lái)實(shí)現(xiàn)。

動(dòng)畫使您的頁(yè)面具有互動(dòng)性;它們引導(dǎo)使用者並使互動(dòng)變得有趣。頁(yè)面上的微小視覺(jué)動(dòng)作,例如用戶互動(dòng)或事件或頁(yè)面導(dǎo)航,給人一種活潑的感覺(jué),就像我們正在與一個(gè)響應(yīng)我們的動(dòng)作的生物進(jìn)行互動(dòng)。

動(dòng)畫,簡(jiǎn)單來(lái)說(shuō),是一種透過(guò)在互動(dòng)或某些事件上隨著時(shí)間的推移更新元素的屬性或尺寸來(lái)視覺(jué)上改變?cè)氐姆绞?。例如,顯示您的操作正在進(jìn)行中的載入指示器。

有兩種方法可以為網(wǎng)頁(yè)上的元素設(shè)定動(dòng)畫(兩種更改元素屬性的方法)。

  1. 透過(guò) CSS,Animate.css 等函式庫(kù)提供了一組可以加入 HTML 元素的動(dòng)畫類別。
  2. 透過(guò) JavaScript,像 Framer Motion 這樣的函式庫(kù)可以在執(zhí)行時(shí)透過(guò)程式碼操縱 DOM 元素的屬性。

在本文中,我們將探索 Framer Motion,這是最受歡迎的動(dòng)畫庫(kù)之一。它提供簡(jiǎn)單性和靈活性,旨在與 React 等現(xiàn)代前端框架配合使用。

為什麼選擇 Framer Motion?

Framer Motion 是一個(gè)用於 React 的生產(chǎn)就緒動(dòng)畫庫(kù),它透過(guò)其聲明性語(yǔ)法創(chuàng)建簡(jiǎn)單的動(dòng)畫(例如過(guò)渡)和複雜的基於手勢(shì)的互動(dòng)。它的特點(diǎn)是:

  • 易於使用: Framer Motion 憑藉其直覺(jué)的 API 和方法,非常簡(jiǎn)單且易於使用。
  • 靈活性:它可用於創(chuàng)建複雜的動(dòng)畫,如平移、拖曳、捏合,或簡(jiǎn)單的動(dòng)畫,如淡入淡出、過(guò)渡。
  • 效能:運(yùn)動(dòng)組件針對(duì)效能進(jìn)行了最佳化,因?yàn)樗鼈冊(cè)?React 生命週期之外渲染,以平穩(wěn)運(yùn)行並確保無(wú)縫的使用者體驗(yàn)。
  • 社群與支援:廣泛的文件、大量範(fàn)例以及社群的廣泛採(cǎi)用使入門變得更加容易。

Framer Motion 入門

使用 npmyarn 套件管理器將 Framer Motion 庫(kù)新增至您的專案中。

npm install framer-motion


npm install framer-motion

載入相依性後,您可以將其包含在專案中以建立互動(dòng)式動(dòng)畫。

yarn add framer-motion

基本概念

運(yùn)動(dòng)組件:

Framer Motion 附帶一系列運(yùn)動(dòng)組件來(lái)創(chuàng)建 120fps 動(dòng)畫。它提供手勢(shì)支持,其中包含所有可使用的特殊 React 元件的 HTML 元素(如motion.div)和常見(jiàn)的 SVG 元素(如motion.square)。

// On Client side
import { motion } from "motion/react"

// On Server-side 
import * as motion from "motion/react-client"

道具與 API:

Framer Motion 提供了一系列 API 作為 props,例如定義動(dòng)畫行為的 initialanimateexit。

<motion.div className="card" />

Initial 屬性在元件掛載時(shí)觸發(fā),animate 在元件更新時(shí)觸發(fā),exit 屬性在元件卸載時(shí)觸發(fā)。有關(guān)更多詳細(xì)信息,請(qǐng)參閱完整的 Framer Motion 動(dòng)畫指南。

運(yùn)動(dòng)元件獨(dú)立於 React 生命週期或渲染週期,以提高效能。因此,我們應(yīng)該依賴 React 狀態(tài)來(lái)實(shí)現(xiàn)動(dòng)畫,而不是使用運(yùn)動(dòng)值來(lái)更新樣式而不觸發(fā)重新渲染。

<motion.button
  initial={{opacity: 0}}
  animate={{opacity: 1}}
  transition={{duration: 1}}
  exit={{opacity: 0}}
>
  Click Me
</motion.button>

變異體包括:

  • listVariants: 定義整個(gè)清單的動(dòng)畫行為,我們?cè)趯⒋嫒∑溆|發(fā)時(shí)的屬性的道具上傳遞變體值。 初始=「隱藏」動(dòng)畫=「可見(jiàn)」。 staggerChildren 確保子元素依序動(dòng)畫。
  • itemVariants: 定義清單項(xiàng)目的動(dòng)畫行為。
  • motion.ulmotion.li 組件繼承變體以創(chuàng)建協(xié)調(diào)的動(dòng)畫。

自訂元件:任何 React 元件都可以透過(guò)將其傳遞給 motion.create() 函數(shù)來(lái)轉(zhuǎn)換為運(yùn)動(dòng)元件。

import { motion, useMotionValue } from "framer-motion";

const MotionState = () => {
  const xPosition = useMotionValue(0);

  useEffect(() => {
    // It won’t trigger a re-render on the component
    const interval = setInterval(() => {
    xPosition.set(xPosition.get() + 100);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <motion.div
   >



<p>In the previous example, the <strong>motion.div</strong> element will be translated by 100px on the x position (horizontally, translateX(100px)) at an interval of 1s.</p>

<p><strong>Variants:</strong> framer-motion provides support for the variants, which allows the reuse of animation configurations across multiple elements.<br>
</p>

<pre class="brush:php;toolbar:false">const AnimatedList = () => {
  const listVariants = {
    hidden: { opacity: 0, y: 20 },
    visible: {
       opacity: 1,
       y: 0,
       transition: {
           staggerChildren: 0.2,
      },
    },
  };

  const itemVariants = {
      visible: { opacity: 1 },
    hidden: { opacity: 0 },
  };

  return (
    <motion.ul initial="hidden" animate="visible" variants={listVariants}>
    {[1, 2, 3].map((item) => (
        <motion.li key={item} variants={itemVariants}>
        Item {item}
        </motion.li>
    ))}
    </motion.ul>
  );
};

預(yù)設(shè)情況下,所有運(yùn)動(dòng)道具在傳遞給 React 元件時(shí)都會(huì)被過(guò)濾掉。動(dòng)畫將應(yīng)用於元件,但您無(wú)法存取 React 中的 props。

要存取運(yùn)動(dòng)道具,請(qǐng)?jiān)诮⑦\(yùn)動(dòng)組件時(shí)傳遞標(biāo)誌 forwardMotionProps: true。

const ReactComponent = (props) => {
  return <button {...props}>ClickMe>/button>;
};

const MotionComponent = motion.create(ReactComponent);

const FadingButton2 = () => {
  return (
    <MotionComponent
    initial={{ opacity: 0 }}
    animate={{ opacity: 1 }}
    exit={{ opacity: 0 }}
    transition={{ duration: 3 }}
    >
    Click Me
    </MotionComponent>
  );
};

motion.create() 函數(shù)也接受一個(gè)字串,該字串將建立自訂 DOM 元素的運(yùn)動(dòng)組件。

const MotionComponent = motion.create(ReactComponent, {
  forwardMotionProps: true,
});

注意:避免在React 生命週期方法中使用motion.create(),因?yàn)檫@會(huì)每次觸發(fā)生命週期方法時(shí)都會(huì)建立一個(gè)新元件。

現(xiàn)在您已經(jīng)了解了 Framer Motion 的工作原理及其 API,讓我們來(lái)看看一些如何將其用於常見(jiàn)動(dòng)畫的範(fàn)例。

範(fàn)例

褪色按鈕

npm install framer-motion
  • initial: 當(dāng)元素不是視口時(shí),設(shè)定按鈕 opacity:0 的初始狀態(tài)。
  • animate: 當(dāng)元素位於視窗中時(shí),將按鈕的狀態(tài)設(shè)為 opacity:1。
  • transition: 配置動(dòng)畫過(guò)渡;該按鈕將需要一秒鐘的時(shí)間從 opacity:0 變?yōu)?opacity:1
  • exit: 設(shè)定元素離開視口時(shí)按鈕的狀態(tài)。

exit 屬性僅在封裝在 AnimatePresence 組件中時(shí)才生效。

yarn add framer-motion

AnimatePresence 影響直接子組件,這些子組件是從 React 組件樹中刪除的運(yùn)動(dòng)組件。

這可能是當(dāng)元件根據(jù)生命週期變更(安裝、更新、卸載)進(jìn)行更新時(shí)

// On Client side
import { motion } from "motion/react"

// On Server-side 
import * as motion from "motion/react-client"

更改

<motion.div className="card" />

孩子被加入到清單中或從清單中刪除。

<motion.button
  initial={{opacity: 0}}
  animate={{opacity: 1}}
  transition={{duration: 1}}
  exit={{opacity: 0}}
>
  Click Me
</motion.button>

滑入式側(cè)邊欄

import { motion, useMotionValue } from "framer-motion";

const MotionState = () => {
  const xPosition = useMotionValue(0);

  useEffect(() => {
    // It won’t trigger a re-render on the component
    const interval = setInterval(() => {
    xPosition.set(xPosition.get() + 100);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <motion.div
   >



<p>In the previous example, the <strong>motion.div</strong> element will be translated by 100px on the x position (horizontally, translateX(100px)) at an interval of 1s.</p>

<p><strong>Variants:</strong> framer-motion provides support for the variants, which allows the reuse of animation configurations across multiple elements.<br>
</p>

<pre class="brush:php;toolbar:false">const AnimatedList = () => {
  const listVariants = {
    hidden: { opacity: 0, y: 20 },
    visible: {
       opacity: 1,
       y: 0,
       transition: {
           staggerChildren: 0.2,
      },
    },
  };

  const itemVariants = {
      visible: { opacity: 1 },
    hidden: { opacity: 0 },
  };

  return (
    <motion.ul initial="hidden" animate="visible" variants={listVariants}>
    {[1, 2, 3].map((item) => (
        <motion.li key={item} variants={itemVariants}>
        Item {item}
        </motion.li>
    ))}
    </motion.ul>
  );
};

過(guò)渡 道具在動(dòng)畫中扮演至關(guān)重要的角色。它們控制動(dòng)畫隨時(shí)間的進(jìn)展方式。 Framer Motion 支援多種屬性以實(shí)現(xiàn)流暢的動(dòng)畫。

  • 持續(xù)時(shí)間: 動(dòng)畫的長(zhǎng)度(以秒為單位)
  • 延遲: 延遲動(dòng)畫的開始(以秒為單位)
  • ease: 一組緩動(dòng)函數(shù),提倡動(dòng)畫如何進(jìn)行(‘ease’、‘easeIn’、‘easeInOut’)

可拖曳模態(tài)

Framer Motion 也支援懸停、點(diǎn)擊和拖曳等手勢(shì)的互動(dòng)式動(dòng)畫。

const ReactComponent = (props) => {
  return <button {...props}>ClickMe>/button>;
};

const MotionComponent = motion.create(ReactComponent);

const FadingButton2 = () => {
  return (
    <MotionComponent
    initial={{ opacity: 0 }}
    animate={{ opacity: 1 }}
    exit={{ opacity: 0 }}
    transition={{ duration: 3 }}
    >
    Click Me
    </MotionComponent>
  );
};

  • Page: 用運(yùn)動(dòng)動(dòng)畫包裹子組件。
  • Initial, animate, & exit: 處理動(dòng)畫出現(xiàn)和消失頁(yè)導(dǎo)航上的組件。
結(jié)論

感謝您的閱讀! Framer Motion 是一個(gè)功能強(qiáng)大的動(dòng)畫庫(kù),可以更輕鬆地在 React 元件中添加令人驚嘆的動(dòng)畫。它可以幫助您創(chuàng)建簡(jiǎn)單的動(dòng)畫來(lái)處理複雜的基於手勢(shì)的互動(dòng)。 Framer Motion 為您的 React 應(yīng)用程式添加互動(dòng)有無(wú)限的可能性。

Essential Studio? 的新版本可在許可證和下載頁(yè)面上供現(xiàn)有客戶使用。如果您是新用戶,請(qǐng)?jiān)]冊(cè)我們的 30 天免費(fèi)試用版以探索我們的功能。

請(qǐng)隨時(shí)透過(guò)我們的支援論壇、支援入口網(wǎng)站或回饋入口網(wǎng)站與我們聯(lián)繫。我們隨時(shí)為您提供協(xié)助!

相關(guān)部落格

  • 可實(shí)現(xiàn)流暢文件處理的前 5 個(gè) React PDF 檢視器
  • 2025 年前 5 名的 React 圖表庫(kù)
  • Vite.js:建立更快的前端
  • React 的 RxJS:解鎖反應(yīng)狀態(tài)

以上是輕鬆的 React 動(dòng)畫:Framer 運(yùn)動(dòng)指南的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語(yǔ)言,各自適用於不同的應(yīng)用場(chǎng)景。 Java用於大型企業(yè)和移動(dòng)應(yīng)用開發(fā),而JavaScript主要用於網(wǎng)頁(yè)開發(fā)。

JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

如何在JS中與日期和時(shí)間合作? 如何在JS中與日期和時(shí)間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時(shí)間處理需注意以下幾點(diǎn):1.創(chuàng)建Date對(duì)像有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設(shè)置時(shí)間信息可用get和set方法,注意月份從0開始;3.手動(dòng)格式化日期需拼接字符串,也可使用第三方庫(kù);4.處理時(shí)區(qū)問(wèn)題建議使用支持時(shí)區(qū)的庫(kù),如Luxon。掌握這些要點(diǎn)能有效避免常見(jiàn)錯(cuò)誤。

為什麼要將標(biāo)籤放在的底部? 為什麼要將標(biāo)籤放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

什麼是在DOM中冒泡和捕獲的事件? 什麼是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。 1.事件捕獲通過(guò)addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委託,提高動(dòng)態(tài)內(nèi)容處理效率;5.捕獲可用於提前攔截事件,如日誌記錄或錯(cuò)誤處理。了解這兩個(gè)階段有助於精確控制JavaScript響應(yīng)用戶操作的時(shí)機(jī)和方式。

JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

如何減少JavaScript應(yīng)用程序的有效載荷大??? 如何減少JavaScript應(yīng)用程序的有效載荷大小? Jun 26, 2025 am 12:54 AM

如果JavaScript應(yīng)用加載慢、性能差,問(wèn)題往往出在payload太大,解決方法包括:1.使用代碼拆分(CodeSplitting),通過(guò)React.lazy()或構(gòu)建工具將大bundle拆分為多個(gè)小文件,按需加載以減少首次下載量;2.移除未使用的代碼(TreeShaking),利用ES6模塊機(jī)制清除“死代碼”,確保引入的庫(kù)支持該特性;3.壓縮和合併資源文件,啟用Gzip/Brotli和Terser壓縮JS,合理合併文件並優(yōu)化靜態(tài)資源;4.替換重型依賴,選用輕量級(jí)庫(kù)如day.js、fetch

See all articles