使用CSS实现按钮边缘跑马灯动画

先来看看效果:

制作过程:

1. 定义标签,a标签是按钮,4个span就是按钮周围那四条行动的蓝边。:

 

1
2
3
4
5
6
7
<a href="https://blog.csdn.net/luo1831251387?spm=1000.2115.3001.5343" class="night" target="blank" rel="noopener">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
          night
    </a>

2. 定义按钮.night的基本样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.night{
            position: relative;
            width: 300px;
            height: 100px;
            color: rgb(18, 190, 243);
            letter-spacing: 12px;
            font-size: 50px;
            line-height: 100px;
            text-align: center;
            /* background-color: rgb(31, 49, 133); */
            background-image: linear-gradient(90deg, rgb(40, 62, 161) 50%,rgb(31, 49, 133) 50% );
            text-transform: uppercase;
            user-select: none;
            text-decoration: none;
            overflow: hidden;
            box-shadow: inset 0 0 10px rgb(14, 20, 105),
            0 0 5px rgb(9, 208, 235);
            transition: all 0.5s;
             
        }<font face="Tahoma"><span> </span></font>

position : 相对定位。

letter-spacing:字间距。

linear-gradient:渐变颜色。

text-transform:全部字母为大写。

user-select:文本不可选中。

text-decoration:消除默认下划线。

overflow:溢出隐藏。

box-shadow:阴影。

transition:过渡效果。

3. 鼠标经过按钮样式变化:

1
2
3
4
5
6
7
8
9
.night:hover{
           text-shadow: 0 0 5px rgb(18, 190, 243),
           0 0 8px rgb(18, 190, 243),
           0 0 10px rgb(18, 190, 243);
           background-image: linear-gradient(90deg, rgb(25, 38, 99) 50%,rgb(13, 22, 58) 50% );
           box-shadow: inset 0 0 10px rgb(14, 20, 105),
           0 0 5px rgb(9, 208, 235),
           0 0 10px rgb(9, 208, 235);
       }

text-shadow:文字阴影。

lineear-gradient:渐变色变化。

box-shadow:盒子阴影变化。

4. 4条span的基本样式:

1
2
3
.night span{
          position: absolute;  
      }

absolute 绝对定位

5. 设置按钮上方那条运动蓝线样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.night span:nth-child(1){
           top: 0;
           left: 0;
           width: 100%;
           height: 2px;
           background-image: linear-gradient(to right, transparent , rgb(0, 153, 255) );
           animation: move1 2s linear infinite;
       }
       @keyframes move1 {
           0%{
               transform: translateX(-100%);
           }
           100%{
               transform: translateX(100%);
           }
       }

linear-gradient:渐变色。

transparent为透明色。

animation:设置动画,让蓝线运动起来。

transform: translateX(-100%); 在X轴上的偏移为-100%。

transform: translateX(100%); 让它偏移到100%。

6. 以此类推,其它三条也设置动画,再让左边和右边那条设置动画延迟便可。:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
.night span:nth-child(2){
            top: 0;
            right: 0;
            width: 2px;
            height: 100%;
            transform: translateY(-100%);
            background-image: linear-gradient(to bottomtransparent rgb(0, 153, 255)  );           
            animation: move2 2s linear infinite;
            animation-delay: 1s;
        }
        @keyframes move2 {
            
            100%{
                transform: translateY(100%);
            }
        }
        .night span:nth-child(3){
            left: 0;
            bottom: 0;
            width: 100%;
            height: 2px;
            background-image: linear-gradient(to left, transparent , rgb(0, 153, 255) );
            animation: move3 2s linear infinite;
        }
        @keyframes move3 {
            0%{
                transform: translateX(100%);
            }
            100%{
                transform: translateX(-100%);
            }
        }
        .night span:nth-child(4){
            top: 0;
            left: 0;
            width: 2px;
            height: 100%;
            transform: translateY(100%);
            background-image: linear-gradient(to top, transparent , rgb(0, 153, 255) );
            animation: move4 2s linear infinite;
            animation-delay: 1s;
        }
        @keyframes move4 {
            100%{
                transform: translateY(-100%);
            }
        }

animation-delay: 1s; 设置动画延迟1秒播放。

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<title>Document</title>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    body{
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgb(4, 4, 19);
    }
    .night{
        position: relative;
        width: 300px;
        height: 100px;
        color: rgb(18, 190, 243);
        letter-spacing: 12px;
        font-size: 50px;
        line-height: 100px;
        text-align: center;
        /* background-color: rgb(31, 49, 133); */
        background-image: linear-gradient(90deg, rgb(40, 62, 161) 50%,rgb(31, 49, 133) 50% );
        text-transform: uppercase;
        user-select: none;
        text-decoration: none;
        overflow: hidden;
        box-shadow: inset 0 0 10px rgb(14, 20, 105),
        0 0 5px rgb(9, 208, 235);
        transition: all 0.5s;
         
    }
    .night:hover{
        text-shadow: 0 0 5px rgb(18, 190, 243),
        0 0 8px rgb(18, 190, 243),
        0 0 10px rgb(18, 190, 243);
        background-image: linear-gradient(90deg, rgb(25, 38, 99) 50%,rgb(13, 22, 58) 50% );
        box-shadow: inset 0 0 10px rgb(14, 20, 105),
        0 0 5px rgb(9, 208, 235),
        0 0 10px rgb(9, 208, 235);
    }
         
    .night span{
        position: absolute;  
    }
    .night span:nth-child(1){
        top: 0;
        left: 0;
        width: 100%;
        height: 2px;
        background-image: linear-gradient(to right, transparent , rgb(0, 153, 255) );
        animation: move1 2s linear infinite;
    }
    @keyframes move1 {
        0%{
            transform: translateX(-100%);
        }
        100%{
            transform: translateX(100%);
        }
    }
    .night span:nth-child(2){
        top: 0;
        right: 0;
        width: 2px;
        height: 100%;
        transform: translateY(-100%);
        background-image: linear-gradient(to bottom,  transparent ,  rgb(0, 153, 255)  );           
        animation: move2 2s linear infinite;
        animation-delay: 1s;
    }
    @keyframes move2 {
        
        100%{
            transform: translateY(100%);
        }
    }
    .night span:nth-child(3){
        left: 0;
        bottom: 0;
        width: 100%;
        height: 2px;
        background-image: linear-gradient(to left, transparent , rgb(0, 153, 255) );
        animation: move3 2s linear infinite;
    }
    @keyframes move3 {
        0%{
            transform: translateX(100%);
        }
        100%{
            transform: translateX(-100%);
        }
    }
    .night span:nth-child(4){
        top: 0;
        left: 0;
        width: 2px;
        height: 100%;
        transform: translateY(100%);
        background-image: linear-gradient(to top, transparent , rgb(0, 153, 255) );
        animation: move4 2s linear infinite;
        animation-delay: 1s;
    }
    @keyframes move4 {
        100%{
            transform: translateY(-100%);
        }
    }
<a href="https://blog.csdn.net/luo1831251387?spm=1000.2115.3001.5343" class="night" target="blank" rel="noopener">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
      night
</a>

到此这篇关于使用CSS实现按钮边缘跑马灯动画的文章就介绍到这了,更多相关CSS按钮跑马灯内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章,希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/navsub/css/13935.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部