搜尋
首頁web前端css教學使用 CSS 建立邊框動畫

使用 CSS 创建边框动画

CSS is used to create beautiful and engaging border animations, which add movement and interest to a web page. To create border animation, we will first need to define aorder for the element animation, we will first need to define border for the element we want to animate, then we'll use CSS transitions and animations to add movement to the border.

Border animations can be used to create hover effects on buttons, links, and other interactive elements. They can also be used to create loading animations that show progress while a page or element is loading animations that show progress on page or -to-action buttons to make them more noticeable.

Approach - 1

we will create a hover effect that animates the border of an element when a user hovers over it.

Algorithm

  • 建立一個HTML文檔,並將標題定義為"Hover Effect Border Animation"。

  • 設定文件的主體,使用flexbox來居中盒子,並給它一個背景顏色為#48b6ff 定義一個有inline-block顯示、10px的padding、18px的字體大小、顏色為#333的box類,並且具有2px的透明實線邊框,過渡時間為0.5s,過渡效果為ease
  • Add a pulsing animation to the border with an infinite duration and ease-in-out timing. 當滑鼠懸停在盒子上時,將邊框從紅色漸變為綠色再到藍色,並禁用脈動動畫

  • Define the pulsing animation with a keyframe that changes the border color from red to green to blue. 在HTML文件的body中新增一個帶有box類別的div元素

  • Save and view the HTML file in a web browser to see the hover effect border animation.

Example

的中文翻譯為:

範例

<!DOCTYPE html>
<html>
<head>
   <title>Hover Effect Border Animation</title>
   <style>
      /* Set up the body with flexbox to center the box */
      body {
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
         background-color: #48b6ff;
         min-height: 100vh;
      }
      /* Style the box with a transparent border */
      .box {
         display: inline-block;
         padding: 10px;
         font-size: 18px;
         color: #333;
         border: 2px solid transparent;
         transition: border 0.5s ease;
         /* Add the pulsing animation to the border */
         animation: border-pulse 2s ease-in-out infinite;
      }
      /* When the box is hovered, change the border to a gradient and disable the pulsing animation */
      .box:hover {
         border-image: linear-gradient(to right, #f00, #0f0, #00f);
         border-image-slice: 1;
         animation: none;
      }
      /* Define the pulsing animation */
      @keyframes border-pulse {
         0% {
            border-color: #f00;
         }
         50% {
            border-color: #0f0;
         }
         100% {
            border-color: #00f;
         }
      }
   </style>
</head>
<body>
   <!-- Add the box element to the HTML -->
   <div class="box">
      Hover over me
   </div>
</body>
</html>

方法 - 2

Here, we will create a loading animation by animating the border of the loading icon.

Algorithm

  • 使用宣告將文件類型宣告為HTML。

  • Start the HTML document by opening the tag.

  • 在標籤內部加上

    標籤。
  • 標籤內,新增一個標籤,並將文件的標題設為"Loading Border Animation"。
  • Add a

  • #Inside the

  • 透過設定其大小、形狀、邊框顏色和動畫屬性,新增一個CSS規則來樣式化載入動畫。

  • 使用 @keyframes 規則建立一個名為 "spin" 的動畫。

  • Add the transform rule to rotate the element 360 degrees.

  • Inside the

    tag, add an
    element with a class of "loading" to display the loading animation.

    Example

    的中文翻譯為:

    範例

    <!DOCTYPE html>
    <html>
    <head>
       <title>Loading Border Animation</title>
       <style>
          /* Styling the body element to center the loading animation */
          body{
             display: flex;
             justify-content: center;
             align-items: center;
             flex-direction: column;
             min-height: 100vh;
             background-color: rgb(253, 114, 114);
          }
    
          /* Styling the loading element */
          .loading {
             width: 50px;
             height: 50px;
             border: 5px solid #ccc;
             border-top-color: #3498db; /* Changing the top border color */
             border-radius: 50%; /* Making the border round */
             animation: spin 1s linear infinite; /* Adding animation to spin continuously */
             margin: 50px auto; /* Centering the element with margin */
          }
    
          /* Defining the animation */
          @keyframes spin {
             to {
                transform: rotate(360deg); /* Rotating the element 360 degrees */
             }
          }
       </style>
    </head>
    <body>
       <div class="loading"></div> <!-- The loading element -->
    </body>
    </html>
    

    Approach - 3

    我們將使用CSS對呼叫到行動按鈕套用邊框動畫。

    Algorithm

    • Create a container and center it.

    • #使用初始為透明的邊框和過渡屬性對元素進行樣式設置,使邊框在0.5秒內動畫化。

    • Create a hover effect for the element that changes the border to a linear gradient of three colors: red, green, and blue.

    • 定義一個名為 "border-pulse" 的關鍵影格動畫,隨著時間的推移改變元素的邊框顏色。

    • 將「border-pulse」動畫套用到元素的初始狀態。

    • When the element is hovered over, disable the "border-pulse" animation by setting it to "none".

    Example

    的中文翻譯為:

    範例

    <!DOCTYPE html>
    <html>
    <head>
       <title>Call to Action Border Animation</title>
       <style>
          /* Set the background color and center content vertically */
          body {
             background-color: #48b6ff;
             font-family: Arial, sans-serif;
             display: flex;
             justify-content: center;
             align-items: center;
             flex-direction: column;
             min-height: 100vh;
          }
          /* Style the button */
          .cta-button {
             display: inline-block;
             position: relative;
             padding: 16px 32px;
             background-color: #ff4d4d;
             color: #fff;
             font-size: 24px;
             text-transform: uppercase;
             text-decoration: none;
             border: none;
             overflow: hidden;
             transition: all 0.4s ease-out;
          }
          /* Add a pseudo-element to create the border animation */
          .cta-button:before {
             content: "";
             display: block;
             position: absolute;
             top: 0;
             left: 50%;
             width: 0;
             height: 100%;
             background-color: #fff;
             transform: translateX(-50%);
             z-index: -1;
             transition: all 0.4s ease-out;
          }
          /* Change the background and text color on hover */
          .cta-button:hover {
             background-color: #fff;
             color: #ff4d4d;
          }
          /* Animate the pseudo-element to create the border animation */
          .cta-button:hover:before {
             width: 110%;
          }
          </style>
    </head>
    <body>
       <a href="#" class="cta-button">Click Me</a>
    </body>
    </html>
    

    Conclusion

    然而,邊框動畫有時可能會導致效能問題,特別是在過度使用或應用於大型元素時,會導致頁面載入時間變慢和整體效能降低。一些較舊的網頁瀏覽器可能不支援某些動畫技術。

    我們也可以使用SVG圖形和JavaScript來建立邊框動畫。它們允許更複雜的動畫,並提供對動畫的更多控制。

以上是使用 CSS 建立邊框動畫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
這麼多顏色鏈接這麼多顏色鏈接Apr 13, 2025 am 11:36 AM

最近有一系列有關顏色的工具,文章和資源。請允許我通過將它們四捨五之後關閉幾個標籤,以供您享受。

自動利潤在Flexbox中的工作方式自動利潤在Flexbox中的工作方式Apr 13, 2025 am 11:35 AM

羅賓以前已經介紹過這一點,但是我在過去的幾周里聽到了一些關於它的困惑,看到另一個人在解釋它,我想

移動彩虹移動彩虹Apr 13, 2025 am 11:27 AM

我絕對喜歡三明治網站的設計。在許多美麗的功能中,這些標題是滾動時帶有彩虹的下線。它不是

新年,新工作?讓我們做一個網格驅動的簡歷!新年,新工作?讓我們做一個網格驅動的簡歷!Apr 13, 2025 am 11:26 AM

許多流行的簡歷設計通過以網格形狀鋪設部分來充分利用可用的頁面空間。讓我們使用CSS網格創建一個佈局

將用戶擺脫過多習慣的一種方法將用戶擺脫過多習慣的一種方法Apr 13, 2025 am 11:25 AM

頁面重新加載是一回事。有時,當我們認為它沒有響應或認為新內容可用時,我們會刷新頁面。有時我們只是生氣

域驅動的設計與React域驅動的設計與ReactApr 13, 2025 am 11:22 AM

關於如何在React世界中組織前端應用的指導很少。 (只需移動文件,直到“感覺正確”,大聲笑)。真相

檢測非活動用戶檢測非活動用戶Apr 13, 2025 am 11:08 AM

大多數情況下,您並不真正在乎用戶是否積極參與或暫時非活動。不活躍,意思,也許他們

Wufoo ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

Wufoo一直在集成方面非常出色。他們與特定應用程序(例如廣告系列顯示器,MailChimp和Typekit)進行集成,但他們也

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器