CSS实现跑马灯效果

发布时间 2023-03-22 21:11:05作者: Happy-P

盒子区域

创建一个大盒子,用来装我们的广告语,还有跑马灯的四个小盒子
<div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        创意广告
    </div>
 
设置box样式
* {
        padding: 0;
        margin: 0;
    }
    
    body {
        background-color: black;
    }
    
    .box {
        width: 100px;
        height: 50px;
        background-color: transparent;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        text-align: center;
        line-height: 50px;
        font-size: 18px;
        font-family: '宋体';
        color: rgb(99, 225, 247);
        overflow: hidden;
    }
 

设置灯光效果

先设置第一个元素的渐变效果,我们这里设置的是顶部的灯光效果,这里我们使用left的属性,设置这个盒子的位置;使用width:100%,使得这个盒子的宽度与大盒子保持一致。
.box :nth-child(1) {
        background: linear-gradient(to right, transparent, #0FF);
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 2px;
    }
第二个子元素
.box :nth-child(2) {
        position: absolute;
        background: linear-gradient(to bottom, transparent, #0FF);
        top: 0;
        right: 0;
        width: 2px;
        height: 100%;
    }
第三个子元素
.box :nth-child(3) {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 2px;
        width: 100%;
        background: linear-gradient(to left, transparent, #0FF);
    }
第四个子元素
.box :nth-child(4) {
        background: linear-gradient(to top, transparent, #0FF);
        position: absolute;
        bottom: 0;
        left: 0;
        width: 2px;
        height: 100%;
    }
 

设置动画效果

我们使用@keyframe属性为我们的灯光添加动画效果
@keyframes run1 {
        0% {
            transform: translateX(-100px);
        }
        100% {
            transform: translateX(100px);
        }
    }
给盒子加上动画
.box :nth-child(1) {
        background: linear-gradient(to right, transparent, #0FF);
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 2px;
        animation: run1 1s linear infinite;
    }
可以设置延迟使效果更加好看,每个盒子比前一个盒子多整体代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跑马灯效果</title>
</head>

<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        创意广告
    </div>
</body>

</html>

<style>
    * {
        padding: 0;
        margin: 0;
    }
    
    body {
        background-color: black;
    }
    
    .box {
        width: 100px;
        height: 50px;
        background-color: transparent;
        position: absolute;
        top: 50%;
        transform: translate(-50%, -50%);
        left: 50%;
        text-align: center;
        line-height: 50px;
        font-size: 18px;
        font-family: '宋体';
        color: rgb(99, 225, 247);
        overflow: hidden;
    }
    
    .box :nth-child(1) {
        background: linear-gradient(to right, transparent, #0FF);
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 2px;
        animation: run1 1s linear infinite;
    }
    
    .box :nth-child(2) {
        position: absolute;
        background: linear-gradient(to bottom, transparent, #0FF);
        top: 0;
        right: 0;
        width: 2px;
        height: 100%;
        animation: run2 1s linear infinite;
        animation-delay: .5s;
    }
    
    .box :nth-child(3) {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 2px;
        width: 100%;
        background: linear-gradient(to left, transparent, #0FF);
        animation: run3 1s linear infinite;
        animation-delay: 1s;
    }
    
    .box :nth-child(4) {
        background: linear-gradient(to top, transparent, #0FF);
        position: absolute;
        bottom: 0;
        left: 0;
        width: 2px;
        height: 100%;
        animation: run4 1s linear infinite;
        animation-delay: 1.5s;
    }
    
    @keyframes run1 {
        0% {
            transform: translateX(-100px);
        }
        100% {
            transform: translateX(100px);
        }
    }
    
    @keyframes run2 {
        0% {
            transform: translateY(-50px);
        }
        100% {
            transform: translateY(50px);
        }
    }
    
    @keyframes run3 {
        0% {
            transform: translateX(100px);
        }
        100% {
            transform: translateX(-100px);
        }
    }
    
    @keyframes run4 {
        0% {
            transform: translateY(50px);
        }
        100% {
            transform: translateY(-50px);
        }
    }
</style>
 

效果图