CSS3动画结合伪元素实现边框滚动效果

  今天和大家分享一个利用CSS3的animation属性完成的一个边框动画效果。大家都知道,CSS3给我们提供了@keyframes关键字,能让我们在网页中轻松插入动画。一个简单的动画插入,结构如下:

复制代码
 1 <style>
 2      .wrap{
 3          position: absolute;
 4          left: 200px;
 5          width: 200px;
 6          height: 200px;
 7          animation: run 2s linear infinite;   /*动画名 时间 速度 动画次数*/
 8          background-color: rgb(87, 182, 9);
 9      }
10      @keyframes run{   /* @keyframe 关键字声明动画 */
11        from{  /* 从什么样子 */
12           left: 200px;
13        }
14        to{
15           left: 500px; /*变成什么样子*/
16         }
17      }
18 </style>
19  <body> 20    <div class="wrap"></div> 21  </body>
复制代码

  效果如下:

  除了用from to 这种形式之外,我们还可以用百分比,更加细化各个阶段的表现样式,例子如下:

复制代码
<head>
  <
style> .around{ position: relative; width:200px; height: 300px; margin:50px auto; background-color:#000005; cursor: pointer; } .around::before,.around::after{ content:''; position: absolute; border-style:solid; animation: move 5s linear infinite; opacity: 1; } .around::after{ animation-delay: -2.5s; } @keyframes move{ 0%{ top:-21px; left:-21px; width: 240px; height: 0; border-width:1px 0 0 0; border-color:#a21; } 25%{ top:-21px; left:-21px; width:0; height:340px; border-width:0 0 0 1px; border-color:rgba(40, 201, 40, 0.904); } 50%{ top:318px; left:-21px; width: 240px; height: 0; border-width:0 0 1px 0; border-color:rgb(204, 74, 14); } 75%{ top:-21px; left:218px; width: 0; height: 340px; border-width:0 1px 0 0; border-color:rgb(160, 170, 17); } 100%{ top:-21px; left:-21px; width: 240px; height: 0; border-width:1px 0 0 0; border-color:rgb(224, 53, 224); } } </style> </head> <body> <div class="around"></div> </body>
复制代码

  效果如下:

  

  这样,我们轻松实现了边框滚动效果,只用了一个div标签,很简单吧

posted @   傻白有点甜  阅读(2150)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示