用CSS3实现边框流动,让你的网页更有创意和个性

前言

边框流动动画是一种非常常见的效果,能够让网页看起来更加生动有趣。通过使用 CSS3,我们可以轻松地实现这种动画效果。本文将介绍如何使用 CSS3 实现边框流动效果,下面一起来看看吧。


实现效果

在这里插入图片描述


实现思路

  1. 首先我们先创建一个盒子容器;
  2. 设置字体样式,并将内容居中;
  3. 使用伪元素 before 创建一个渐变盒子;
  4. 使用伪元素 after 再创建一个盒子;
  5. 然后给渐变盒子设置旋转动画;
  6. 最后隐藏盒子以外的内容。

完整源码

<template>
  <div>
    <div class="boxes">
      <p>css</p>
    </div>
  </div>
</template>
<style scoped>
.boxes {
  position: relative;
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}
.boxes p {
  color: #fff;
  font-size: 24px;
  z-index: 1;
}
.boxes::before {
  content: "";
  position: absolute;
  width: 100px;
  height: 140%;
  background: linear-gradient(rgb(24, 98, 235), rgb(229, 66, 6));
}
.boxes::after {
  content: "";
  position: absolute;
  background: #0e1532;
  inset: 4px; /* inset 是 top、bottom、left、right 都为5px的简写*/
  border-radius: 16px;
}
.boxes::before {
  animation: rotate 4s linear infinite;
}
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>

或者,你还可以这样:

在这里插入图片描述

完整源码

<template>
  <div class="card">
    <div class="bg"></div>
    <div class="blob"></div>
  </div>

</template>
<style scoped>
.card {
  position: relative;
  width: 200px;
  height: 250px;
  border-radius: 14px;
  z-index: 1111;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
}

.bg {
  position: absolute;
  top: 5px;
  left: 5px;
  width: 190px;
  height: 240px;
  z-index: 2;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(24px);
  border-radius: 10px;
  overflow: hidden;
  outline: 2px solid white;
}

.blob {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background-color: #ff0000;
  opacity: 1;
  filter: blur(12px);
  animation: blob-bounce 5s infinite ease;
}

@keyframes blob-bounce {
  0% {
    transform: translate(-100%, -100%) translate3d(0, 0, 0);
  }

  25% {
    transform: translate(-100%, -100%) translate3d(100%, 0, 0);
  }

  50% {
    transform: translate(-100%, -100%) translate3d(100%, 100%, 0);
  }

  75% {
    transform: translate(-100%, -100%) translate3d(0, 100%, 0);
  }

  100% {
    transform: translate(-100%, -100%) translate3d(0, 0, 0);
  }
}
</style>
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用CSS3中的动画属性和边框属性来实现边框线条流动动画。以下是一个基本的示例代码: ``` <!DOCTYPE html> <html> <head> <title>CSS3 Border Animation</title> <style> .border-animation { width: 200px; height: 200px; border: 3px solid #000; animation: border-anim 2s infinite; } @keyframes border-anim { 0% { border-top: 3px solid #000; border-right: 3px solid #000; border-bottom: none; border-left: none; } 25% { border-top: none; border-right: 3px solid #000; border-bottom: 3px solid #000; border-left: none; } 50% { border-top: none; border-right: none; border-bottom: 3px solid #000; border-left: 3px solid #000; } 75% { border-top: 3px solid #000; border-right: none; border-bottom: none; border-left: 3px solid #000; } 100% { border-top: 3px solid #000; border-right: 3px solid #000; border-bottom: 3px solid #000; border-left: none; } } </style> </head> <body> <div class="border-animation"></div> </body> </html> ``` 在上面的代码中,我们定义了一个样式类`.border-animation`,并设置了其宽度、高度和3像素的黑色边框。接着,我们使用CSS3中的`animation`属性来定义动画,指定了`border-anim`关键帧动画和2秒的动画持续时间,并设置`infinite`表示无限循环播放。 在关键帧动画`border-anim`中,我们使用了五个关键帧来控制边框线条的变化。每个关键帧中,我们可以指定边框的样式和颜色,以及哪些边框线条需要显示或隐藏。 这个例子中,我们通过不同的组合方式来交替显示边框线条,从而实现流动动画的效果。你可以根据需要调整关键帧中的样式和时间间隔来实现不同的动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水星记_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值