搜索
首页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 a 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. We can also use border animations on call-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删除
每周平台新闻:Galaxy Store的Web应用程序,Tappable Stories,CSS Subgrid每周平台新闻:Galaxy Store的Web应用程序,Tappable Stories,CSS SubgridApr 14, 2025 am 11:20 AM

在本周的综述中:Firefox获得了类似锁匠的力量,三星的Galaxy Store开始支持Progressive Web Apps,CSS Subgrid正在Firefox发货

每周平台新闻:Internet Explorer模式,搜索控制台中的速度报告,限制通知提示每周平台新闻:Internet Explorer模式,搜索控制台中的速度报告,限制通知提示Apr 14, 2025 am 11:15 AM

在本周的综述中:Internet Explorer进入Edge,Google Search Console吹捧新的速度报告,Firefox提供了Facebook&#039;

CSS自定义属性的范围的功率(和乐趣)CSS自定义属性的范围的功率(和乐趣)Apr 14, 2025 am 11:11 AM

您可能至少已经对CSS变量有点熟悉了。如果没有,这是两秒钟的概述:它们真的称为自定义属性

我们是程序员我们是程序员Apr 14, 2025 am 11:04 AM

构建网站正在编程。编写HTML和CSS正在编程。我是程序员,如果您在这里阅读CSS-Tricks,那么您很有可能是您

您如何从网站上删除未使用的CSS?您如何从网站上删除未使用的CSS?Apr 14, 2025 am 10:59 AM

在这里,我想预先知道的是:这是一个很难的问题。如果您降落在这里,因为您希望指向您可以运行的工具

图片中的图片网络API简介图片中的图片网络API简介Apr 14, 2025 am 10:57 AM

图片中的图片在2016年发行了Macos Sierra,在Safari浏览器中首次出现在网络上。这使用户可以弹出

使用Gatsby组织和准备图像以使图像模糊效果的方法使用Gatsby组织和准备图像以使图像模糊效果的方法Apr 14, 2025 am 10:56 AM

盖茨比(Gatsby)进行了出色的处理和处理图像。例如,它可以帮助您节省图像优化的时间,因为您不必手动

哦,嘿,填充百分比基于父元素的宽度哦,嘿,填充百分比基于父元素的宽度Apr 14, 2025 am 10:55 AM

今天,我学到了一些有关基于百分比的(%)填充的知识,我脑海中完全错了!我一直认为百分比填充是基于

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.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中