使用 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