Rosa
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CSS 動畫效果 @keyframes animation ###### tags: `CSS` [TOC] --- ## :star:關鍵影格動畫效果 @keyframes & animation * @keyframes 需搭配 animation 服用 * 不可單獨使用數字,一定要加入 % * 如果同樣的百分比,則相同屬性會後蓋前 ==語法格式↓== :::info /* 動畫設定 */   @keyframes 自訂名稱 {     from { background-color: yellow;}     to { background-color: red;}   } /* 動畫關鍵影格的漸層設定 */   @keyframes 自訂名稱 {    0% { top: 0px;}    25% { top: 150px;}    50% { top: 300px;}    75% {top: 150px;}    100% {top: 0px;}   } ::: :::warning :fire: 滑鼠移入後停止     animation-play-state:paused; ::: #### ==動畫名稱 animation-name:== * 設定動畫名稱,預設值是「none」 * 名稱可以加引號、亦可不加引號 ex:@keyframes oxxo { ... }   @keyframes "oxxo" { ... } * 如果同名,以後面的名稱為主 * 名稱大小寫有區別 * 特殊字元不能使用,但是用引號框起來就可以 #### ==動畫持續時間 animation-duration:== * 動畫單次播放的時間,預設 0 ,單位為 s 或 ms * 和 animation-name都是屬於一定要有的屬性 #### ==動畫加速度函式(屬性) animation-timing-function:== * 動畫轉換速度,預設為「ease」 ![](https://i.imgur.com/Cly9ewh.png) * 較少用的語法: * step-start (看不到第一格) * step-end (看不到最後一格) ![](https://i.imgur.com/fPk4qDp.gif) * steps(int,start|end) 以上2種的進化型語法 * cubic-bezier(n,n,n,n) 以上語法可參考:https://www.oxxostudio.tw/articles/201803/css-animation.html #### ==動畫延遲時間 animation-delay:== * 動畫要延後多久才開始播放,預設 0 ,單位為 s 或 ms * 如果設定為「負值」,例如 -1s、-2s,得到的結果「**不會延遲,而是快轉**」 ex:一段動畫要 5 秒,animation-delay 設定為 -2s,那麼動畫將會直接從第二秒的位置開始播放,播放三秒後停止 ( 類似 5-2=3 的概念 )。 #### ==動畫播放次數 animation-iteration-count:== * 需設定大於0的數字,預設1,設定「**infinite**」表示無限重複播放 #### ==動畫播放方向 animation-direction:== ![](https://i.imgur.com/mFjh30d.png) * **normal**:正常播放,從 0% 到 100% (預設值) * **reverse**:反轉播放,從 100% 到 0% * **alternate**:正反轉輪流播放 奇數次為 0% 到 100%,偶數次為 100% 到 0% 若動畫播放次數只有一次就只會正常播放 * **alternate-reverse**:alternate的相反,奇數次為 100% 到 0%,偶數次為 0% 到 100%,若動畫播放次數只有一次就只會反轉播放。 #### ==動畫播放狀態 animation-play-state:== * **running**,預設值,表示動畫播放中 * **paused**,表示暫停 #### ==動畫播放前後模式(屬性) animation-fill-mode:== * **none**:預設值,不論動畫播放次數,結束後一律返回原始狀態。 * **forwards**:動畫結束後,保持在最後一個影格狀態。 * **backwards**:動畫結束後,保持在第一個影格狀態 ( 但實際測試和 none 效果一樣 )。 * **both**:依據動畫的次數或播放方向,保持在第一個影格或最後一個影格狀態,相當實用。 ### 總表整理如下: ![](https://i.imgur.com/v1E2yjW.png) :::danger 這八種屬性,亦可透過 **animation** 的屬性,做簡短的縮寫,用法如下: animation:name duration | timing-function | delay | iteration-count | direction | fill-mode | play-state; 縮寫除了在代碼上簡短許多,更可以讓「同一個元素套用多組動畫」,用法只需要在後方用逗點分隔即可 ::: --- ## Animation Events * animationstart:當動畫開始。 * animationend:當動畫結束。 * animationiteration:當動畫重複。 * animationcancel:當動畫中止 ( 目前還不支援 )。 練習:變色圓圈圈 ```htmlmixed= <title>animation動畫效果-變色圓圈圈</title> <style> @keyframes myanimation{ from { background-color: #55555; } to { background-color: #428bca; } } .box { width: 150px; height: 150px; background-color: lightgreen; border-radius: 50%; border: 1px dotted black; text-align: center; line-height: 150px; position: absolute; animation-name: myanimation; animation-duration: 2s; animation-timing-function: ease; animation-delay: 1s; } </style> </head> <body> <div class="box">light</div> </body> ``` 顯示如下: ![](https://i.imgur.com/ph39w6M.gif) --- 練習:抓方塊 ```htmlmixed= <title>animation動畫效果-抓方塊</title> <style> /*抓方塊*/ @keyframes ssanimation{ from { left: 10px; } to { left: 300px; } } .box2 { width: 100px; height: 100px; background-color: #428bca; border: 1px dotted black; text-align: center; line-height: 100px; position: absolute; animation-name: ssanimation; animation-duration: 1s; animation-timing-function: ease-in-out; animation-direction: alternate; animation-iteration-count: infinite; } .box2:hover { animation-play-state: paused; } </style> </head> <body> <!-- 抓方塊 --> <div class="box2">box2</div> </body> ``` 顯示如下: ![](https://i.imgur.com/mgrLW7q.gif) --- 練習:來回繞圈的方形 ```htmlmixed= <title>animation動畫效果-來回繞圈的方形</title> <style> /*方形繞圈圈*/ @keyframes squarecircle { 0% { background-color: #23C89C; left:10px; top:10px;} 25% { background-color: #F1CF3A; left:310px; top:10px;} 50% { background-color: #4DB3CE; left: 310px; top:210px;} 75% { background-color: #F9B885; left: 10px; top:210px;} 100% { background-color: #F76C91; left:10px; top:10px;} } .box3 { width: 100px; height: 100px; background-color: #BBE1F5; border: 10px dotted ##FCF8A7; text-align: center; line-height: 100px; position: absolute; animation: squarecircle 4s ease-in-out alternate infinite; } </style> </head> <body> <!-- 方形繞圈圈 --> <div class="box3">來回繞的方形</div> </body> ``` 顯示如下: ![](https://i.imgur.com/jhHLOZH.gif)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully