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

首頁 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)畫庫 Framer Motion 的全面指南。它涵蓋了運(yùn)動(dòng)組件、變體和過渡等關(guān)鍵概念,并提供了創(chuàng)建淡入淡出按鈕、滑入側(cè)邊欄、可拖動(dòng)模式和卡片翻轉(zhuǎn)動(dòng)畫的實(shí)際示例。

作為前端開發(fā)人員,我們的首要任務(wù)是創(chuàng)建讓用戶參與的 Web 應(yīng)用程序。這可以通過創(chuàng)建交互式頁面并提供更好的用戶體驗(yàn)來實(shí)現(xiàn)。

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

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

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

  1. 通過 CSS,Animate.css 等庫提供了一組可以添加到 HTML 元素的動(dòng)畫類。
  2. 通過 JavaScript,像 Framer Motion 這樣的庫可以在運(yùn)行時(shí)通過代碼操縱 DOM 元素的屬性。

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

為什么選擇 Framer Motion?

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

  • 易于使用: Framer Motion 憑借其直觀的 API 和方法,非常簡(jiǎn)單且易于使用。
  • 靈活性:它可用于創(chuàng)建復(fù)雜的動(dòng)畫,如平移、拖動(dòng)、捏合,或簡(jiǎn)單的動(dòng)畫,如淡入淡出、過渡。
  • 性能:運(yùn)動(dòng)組件針對(duì)性能進(jìn)行了優(yōu)化,因?yàn)樗鼈冊(cè)?React 生命周期之外渲染,以平穩(wěn)運(yùn)行并確保無縫的用戶體驗(yàn)。
  • 社區(qū)和支持:廣泛的文檔、大量示例以及社區(qū)的廣泛采用使入門變得更加容易。

Framer Motion 入門

使用 npmyarn 包管理器將 Framer Motion 庫添加到您的項(xiàng)目中。

npm install framer-motion

或者

npm install framer-motion

加載依賴項(xiàng)后,您可以將其包含在項(xiàng)目中以創(chuàng)建交互式動(dòng)畫。

yarn add framer-motion

基本概念

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

Framer Motion 附帶一系列運(yùn)動(dòng)組件來創(chuàng)建 120fps 動(dòng)畫。它提供手勢(shì)支持,其中包含所有可以使用的特殊 React 組件的 HTML 元素(如motion.div)和常見的 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)來實(shí)現(xiàn)動(dòng)畫,而不是使用運(yùn)動(dòng)值來更新樣式而不觸發(fā)重新渲染。

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

變體包括:

  • listVariants: 定義整個(gè)列表的動(dòng)畫行為,我們?cè)趯⒃L問其觸發(fā)時(shí)的屬性的道具上傳遞變體值。 初始=“隱藏”動(dòng)畫=“可見”。 staggerChildren 確保子元素按順序動(dòng)畫。
  • itemVariants: 定義列表項(xiàng)的動(dòng)畫行為。
  • motion.ulmotion.li 組件繼承變體以創(chuàng)建協(xié)調(diào)的動(dòng)畫。

自定義組件:任何 React 組件都可以通過將其傳遞給 motion.create() 函數(shù)來轉(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>
  );
};

默認(rèn)情況下,所有運(yùn)動(dòng)道具在傳遞給 React 組件時(shí)都會(huì)被過濾掉。動(dòng)畫將應(yīng)用于組件,但您無法訪問 React 中的 props。

要訪問運(yùn)動(dòng)道具,請(qǐng)?jiān)趧?chuàng)建運(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è)字符串,該字符串將創(chuàng)建自定義 DOM 元素的運(yùn)動(dòng)組件。

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

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

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

示例

褪色按鈕

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)畫過渡;該按鈕將需要一秒鐘的時(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>
  );
};

過渡 道具在動(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’)

可拖動(dòng)模態(tài)

Framer Motion 還支持懸停、點(diǎn)擊和拖動(dòng)等手勢(shì)的交互式動(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)和消失頁面導(dǎo)航上的組件。

結(jié)論

感謝您的閱讀! Framer Motion 是一個(gè)功能強(qiáng)大的動(dòng)畫庫,可以更輕松地向 React 組件添加令人驚嘆的動(dòng)畫。它可以幫助您創(chuàng)建簡(jiǎn)單的動(dòng)畫來處理復(fù)雜的基于手勢(shì)的交互。 Framer Motion 為您的 React 應(yīng)用程序添加交互有無限的可能性。

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

請(qǐng)隨時(shí)通過我們的支持論壇、支持門戶或反饋門戶與我們聯(lián)系。我們隨時(shí)為您提供幫助!

相關(guān)博客

  • 可實(shí)現(xiàn)流暢文檔處理的前 5 個(gè) React PDF 查看器
  • 2025 年排名前 5 的 React 圖表庫
  • Vite.js:構(gòu)建更快的前端
  • 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)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系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脫衣機(jī)

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

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

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

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

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

JavaScript評(píng)論:簡(jiǎn)短說明 JavaScript評(píng)論:簡(jiǎn)短說明 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)格式化日期需拼接字符串,也可使用第三方庫;4.處理時(shí)區(qū)問題建議使用支持時(shí)區(qū)的庫,如Luxon。掌握這些要點(diǎ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)

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

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

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

事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。1.事件捕獲通過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ī)和方式。

Java和JavaScript有什么區(qū)別? Java和JavaScript有什么區(qū)別? Jun 17, 2025 am 09:17 AM

Java和JavaScript是不同的編程語言。1.Java是靜態(tài)類型、編譯型語言,適用于企業(yè)應(yīng)用和大型系統(tǒng)。2.JavaScript是動(dòng)態(tài)類型、解釋型語言,主要用于網(wǎng)頁交互和前端開發(fā)。

See all articles