使用CSS创建边框动画


CSS用于创建美观且引人入胜的边框动画,为网页增添动感和趣味。要创建边框动画,我们首先需要为要动画的元素定义边框,然后使用CSS过渡和动画为边框添加动效。

边框动画可用于为按钮、链接和其他交互式元素创建悬停效果。它们还可用于创建加载动画,在页面或元素加载时显示进度。我们还可以将边框动画用于号召性用语按钮,以使其更醒目。

方法一

我们将创建一个悬停效果,当用户将鼠标悬停在元素上时,该效果会为元素的边框设置动画。

算法

  • 创建一个HTML文档,并将标题设置为“悬停效果边框动画”。

  • 使用flexbox设置文档的主体,以居中显示框并为其提供#48b6ff的背景颜色。

  • 定义一个box类,该类具有内联块显示、10px的填充、18px的字体大小、#333的颜色和一个2px实心透明边框,该边框以0.5s的缓动过渡。

  • 使用无限持续时间和缓入缓出时间为边框添加脉冲动画。

  • 当鼠标悬停在框上时,将边框更改为从红色到绿色到蓝色的渐变,并禁用脉冲动画。

  • 使用关键帧定义脉冲动画,该关键帧将边框颜色从红色更改为绿色再更改为蓝色。

  • 将具有box类的div元素添加到HTML文档的主体。

  • 保存并在Web浏览器中查看HTML文件,以查看悬停效果边框动画。

示例

<!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>

方法二

在这里,我们将通过为加载图标的边框设置动画来创建加载动画。

算法

  • 使用<!DOCTYPE html>声明将文档类型声明为HTML。

  • 通过打开<html>标签开始HTML文档。

  • 在<html>标签内添加<head>标签。

  • 在<head>标签内,添加<title>标签,并将文档的标题指定为“加载边框动画”。

  • 在<head>标签内添加<style>标签,以向文档添加样式。

  • 在<style>标签内,添加CSS规则以设置<body>元素的样式,包括居中加载动画和设置背景颜色。

  • 添加CSS规则以通过设置其大小、形状、边框颜色和动画属性来设置加载动画的样式。

  • 使用@keyframes规则创建一个名为“spin”的动画。

  • 添加transform规则以将元素旋转360度。

  • 在<body>标签内,添加一个具有“loading”类的<div>元素以显示加载动画。

示例

<!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>

方法三

我们将使用CSS将边框动画应用于号召性用语按钮。

算法

  • 创建一个容器并将其居中。

  • 使用边框设置元素的样式,该边框最初是透明的,并且过渡属性会在0.5秒内为边框设置动画。

  • 为元素创建悬停效果,该效果将边框更改为三种颜色的线性渐变:红色、绿色和蓝色。

  • 定义一个名为“border-pulse”的关键帧动画,该动画会随着时间的推移更改元素的边框颜色。

  • 最初将“border-pulse”动画应用于元素。

  • 当鼠标悬停在元素上时,通过将其设置为“none”来禁用“border-pulse”动画。

示例

<!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>

结论

但是,边框动画有时会导致性能问题,尤其是在过度使用或用于大型元素时,这会导致页面加载时间变慢并降低整体性能。某些旧版Web浏览器可能不支持某些动画技术。

我们还可以使用SVG图形和JavaScript创建边框动画。它们允许更复杂的动画,并提供对动画的更多控制。

更新于:2023年5月22日

4K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.