css动画效果(边框线条流动效果)

1.整体效果

https://mmbiz.qpic.cn/sz_mmbiz_gif/EGZdlrTDJa6FxrVbiamfvb7b0H4qcDzZRwq3PqvXfuMDaPZ44VUic1h2WRZvI0eJ8F9XTgpbzeAu1fq3OAvcpgsg/640?wx_fmt=gif&from=appmsg&wxfrom=13

在网页设计中,边框往往被视作静态的容器,但在CSS的魔法下,它们可以变得生动而富有动感。CSS边框线条流动效果是一种创新的视觉技术,它能够让边框看起来像是在流动或变化,为网页添加一种独特的动态美感。

2.完整代码

HTML

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>动态边框</title>  
</head>  
<body>  
<div class="container">  
  
    <div class="box">  
        HELLO WORLD  
        <span class="line"></span>  
        <span class="line"></span>  
        <span class="line"></span>  
        <span class="line"></span>  
    </div>  
</div>  
</body>  
</html>

CSS

.container {  
    width: 800px;  
    height: 680px;  
    margin: 20px auto;  
    /* border: 1px solid green; */  
    position: relative;  
}  
.box {  
    width: 420px;  
    height: 210px;  
    margin: 100px auto;  
    line-height: 210px;  
    text-align: center;  
    font-size: 40px;  
    position: relative;  
    overflow: hidden;  
}  
  
.line {  
    position: absolute;  
}  
  
.line:nth-child(1) {  
    top: 0;  
    left: 0;  
    width: 100%;  
    height: 8px;  
    background: linear-gradient(90deg, transparent, rgb(234, 249, 158));  
    animation: animate1 8s linear infinite;  
}  
  
  
@keyframes animate1 {  
    0% {  
        left: -100%;  
    }    50%,  
    100% {  
        left: 100%;  
    }}  
  
.line:nth-child(2) {  
    top: -100%;  
    right: 0;  
    width: 8px;  
    height: 100%;  
    background: linear-gradient(180deg, transparent, rgb(160, 245, 250));  
    animation: animate2 8s linear infinite;  
    /* 注意要加上延时触发动画效果,这样线条才会依次触发 */    animation-delay: 2s;  
}  
  
@keyframes animate2 {  
    0% {  
        top: -100%;  
    }  
    50%,  
    100% {  
        top: 100%;  
    }}  
  
.line:nth-child(3) {  
    bottom: 0;  
    right: 0;  
    width: 100%;  
    background: linear-gradient(270deg, transparent, rgb(245, 58, 220));  
    animation: animate3 8s linear infinite;  
    animation-delay: 4s;  
}  
  
@keyframes animate3 {  
    0% {  
        right: -100%;  
        height: 8px;  
    }  
    50%,  
    100% {  
        height: 8px;  
        right: 100%;  
    }}  
  
.line:nth-child(4) {  
    bottom: -100%;  
    left: 0;  
    width: 8px;  
    height: 100%;  
    background: linear-gradient(360deg, transparent, rgb(254, 138, 49));  
    animation: animate4 8s linear infinite;  
    animation-delay: 6s;  
}  
  
@keyframes animate4 {  
    0% {  
        bottom: -100%;  
    }  
    50%,  
    100% {  
        bottom: 100%;  
    }}

3.关键点

CSS边框线条流动效果是有以下几个关键点:

  1. CSS盒模型(Box Model):通过设置widthheightmargin等属性来控制元素的大小和位置。

  2. CSS定位(Positioning):使用position: relative;position: absolute;来设置元素的定位方式,使得线条能够相对于小盒子定位。

  3. CSS渐变(Gradients):使用linear-gradient函数创建线条的渐变背景。

  4. CSS动画(Animations):使用@keyframes定义动画,并通过animation属性应用到元素上,实现线条的动态效果。

  5. CSS伪类选择器(Pseudo-class selectors):使用:nth-child伪类选择器来选择特定的线条元素,并对它们应用不同的样式。

  6. CSS动画延时(Animation delay):通过animation-delay属性设置动画的延时时间,使得线条动画能够依次开始。

  7. CSS动画无限循环(Infinite animations):通过设置animation-iteration-count: infinite;使得动画无限次地重复。

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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、付费专栏及课程。

余额充值