Xin's blog Xin's blog
首页
  • 前端文章

    • HTML
    • CSS
    • JavaScript
    • Vue
    • 组件与插件
    • CSS扩展语言
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • 《Git》学习笔记
    • TypeScript笔记
    • JS设计模式总结笔记
  • 前端框架面试题汇总
  • 基本面试题
  • 进阶面试题
  • 其它
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 前后端联调
  • mock.js
  • 奇技淫巧
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)

Xin

英雄可不能临阵脱逃啊~
首页
  • 前端文章

    • HTML
    • CSS
    • JavaScript
    • Vue
    • 组件与插件
    • CSS扩展语言
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • 《Git》学习笔记
    • TypeScript笔记
    • JS设计模式总结笔记
  • 前端框架面试题汇总
  • 基本面试题
  • 进阶面试题
  • 其它
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 前后端联调
  • mock.js
  • 奇技淫巧
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)
  • HTML

  • CSS

    • 知识点

    • 案例

      • flex布局案例-网格布局
      • flex 布局,让某个子元素靠右对齐
      • transform案例
      • 文字在一行或两行时超出显示省略号
      • CSS智慧阴影
      • css3渐变的使用
        • 渐变边框
        • 边框流光
        • 流光按钮
      • CSS3模糊的使用
      • 扫光特效
      • 「布局技巧」图片未加载前自动撑开元素高度
      • 水平垂直居中的几种方式-案例
      • 如何根据系统主题自动响应CSS深色模式
      • 「css技巧」使用hover和attr()定制悬浮提示
  • JavaScript

  • Vue

  • 组件与插件

  • css扩展语言

  • 学习笔记

  • 前端
  • CSS
  • 案例
ctrlwin
2021-08-11

css3渐变的使用

# CSS渐变的应用

# 渐变边框

效果展示:

<html>
	<div class="border-image-pesudo">按钮</div>
</html>

<style>
.border-image-pesudo {
    font-size: 18px;
    color: #ffffff;
    text-align: center;
    cursor: pointer; 
    width: 144px;
    height: 48px;
    line-height: 48px; 
    /*background-image: linear-gradient(-33deg, rgba(254,187,5,0.80) 0%, rgba(254,138,60,0.80) 100%);
    box-shadow: 0px 6px 12px 0px rgba(255,93,26,0.2);*/
    background: #FFADD2;
    border: 1px transparent solid;
    border-radius: 31px;
    position: relative;
}

.border-image-pesudo::before {
    content: '';
    position: absolute;
    top: -5px;
    bottom: -5px;
    left: -5px;
    right: -5px;
    border-radius: 31px;
    background-image: linear-gradient(135deg, rgba(8,151,156,0.94) 0%, rgba(24, 144, 255, 0.93) 50%, rgba(255, 77, 79, 0.98) 100%);
    z-index: -1;
}
</style>
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

点右下角Codepen按钮查看效果。

这里示例不知道为啥没效果,但在浏览器可以正常运行的

具体代码:

<div class="border-image-pesudo">按钮</div>
1
.border-image-pesudo {
    font-size: 18px;
    color: #ffffff;
    text-align: center;
    cursor: pointer; 
    width: 144px;
    height: 48px;
    line-height: 48px; 
    background: #FFADD2;
    border: 1px transparent solid;
    border-radius: 31px;
    position: relative;
}

.border-image-pesudo::before {
    content: '';
    position: absolute;
    top: -5px;
    bottom: -5px;
    left: -5px;
    right: -5px;
    border-radius: 31px;
    background-image: linear-gradient(135deg, rgba(8,151,156,0.94) 0%, rgba(24, 144, 255, 0.93) 50%, rgba(255, 77, 79, 0.98) 100%);
    z-index: -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

# 边框流光

效果展示:

<html>
	<div class="content-anim">
      <div class="anim1" id="anim"></div>
	</div>
    <button onclick="document.getElementById('anim').classList.add('bgcolor')">查看实现原理</button>
    <button onclick="document.getElementById('anim').classList.remove('bgcolor')">重置</button>
</html>

<style>
.content-anim{
  width: 550px;
  height: 280px;
  background-image: linear-gradient(90deg, rgba(255, 173, 210,0.80) 0%, rgba(247, 89, 171) 100%);
  border-radius: 32px;
  position: relative;
  margin: 10px auto;
}
.content-anim .anim1 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: all .3s;
}

.content-anim .anim1::before {
    content: "";
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    border: 5px solid #fff;
    transition: all .5s;
    border-radius: 32px;
    animation: clippath 4s infinite linear;
}
#anim.bgcolor::before{
	background: #fff;
}

@keyframes clippath {

  0%,
  100% {
      clip-path: inset(0 0 98% 0); 
  }

  25% {
      clip-path: inset(0 98% 0 0);
  }

  50% {
      clip-path: inset(98% 0 0 0);
  }

  75% {
      clip-path: inset(0 0 0 98%);
  }
}
</style>
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

边框流光效果使用到了CSS3的一个属性:clip-path。它使用指定的裁剪方式创建元素的可显示区域,详情请看这里 (opens new window)。

inset是按照矩形区域进行裁剪。

具体代码:

<div class="content">
    <div class="anim"></div>
</div>
1
2
3
.content{
  width: 550px;
  height: 280px;
  background-image: linear-gradient(90deg, rgba(254,187,5,0.80) 0%, #FE8A3C 100%);
  border-radius: 32px;
  position: relative;
  /*transform: translate(0,0);*/
}
.anim {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: all .3s;
  /*z-index: -1;*/ /*如果父级没有使用transform:translate()那么z-index:-1也不用加*/
}

.anim::before {
  content: "";
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  border: 5px solid #fff;
  transition: all .5s;
  border-radius: 32px;
  animation: clippath 12s infinite linear;
}

@keyframes clippath {

  0%,
  100% {
      clip-path: inset(0 0 98% 0);
  }

  25% {
      clip-path: inset(0 98% 0 0);
  }

  50% {
      clip-path: inset(98% 0 0 0);
  }

  75% {
      clip-path: inset(0 0 0 98%);
  }
}
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

# 流光按钮

效果展示:

<html>
	 <a href="#" class="colorBtn">button</a>
</html>

<style>
.colorBtn {
    position: relative;
    display: inline-block;
    width: 230px;
    height: 90px;
    line-height: 90px;
    text-align: center;
    color: #fff;
    font-size: 25px;
    text-transform: uppercase;
    cursor: pointer;
    background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
    background-size: 400%;
    border-radius: 60px;
  }

  .colorBtn:hover {
    text-decoration: none!important;
    animation: animate 8s linear infinite;
  }

  .colorBtn::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -5px;
    right: -5px;
    bottom: -5px;
    z-index: -1;
    background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
    background-size: 400%;
    border-radius: 40px;
    opacity: 0;
    transition: 0.5s;
  }

  .colorBtn:hover::before {
    filter: blur(20px);
    opacity: 1;
    animation: animate 8s linear infinite;
  }
    @keyframes animate {
        0% {
            background-position: 0%;
        }

        100% {
            background-position: 400%;
        }
    }
</style>
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

具体代码:

<body>
  <a href="#" class="colorBtn">button</a>
</body>
1
2
3
<style>
  .colorBtn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 230px;
    height: 90px;
    line-height: 90px;
    text-align: center;
    color: #fff;
    font-size: 25px;
    text-transform: uppercase;
    cursor: pointer;
    background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
    background-size: 400%;
    border-radius: 60px;
  }

  .colorBtn:hover {
    animation: animate 8s linear infinite;
    text-decoration: none !important;
  }

  .colorBtn::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -5px;
    right: -5px;
    bottom: -5px;
    z-index: -1;
    background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
    background-size: 400%; /*将背景色放大,再通过动画移动背景就实现了流光效果*/
    border-radius: 40px;
    opacity: 0;
    transition: 0.5s;
  }

  .colorBtn:hover::before {
    filter: blur(20px);
    opacity: 1;
    animation: animate 8s linear infinite;
  }
  @keyframes animate {
    0% {
      background-position: 0%;
    }

    100% {
      background-position: 400%;
    }
  }
</style>
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
在GitHub上编辑 (opens new window)
上次更新: 1/20/2022, 11:27:12 AM

← CSS智慧阴影 CSS3模糊的使用→

最近更新
01
createElement函数创建虚拟DOM
05-26
02
clipboard 剪切板属性
05-26
03
vue的权限管理
05-16
更多文章>
Theme by Vdoing | Copyright © 2021-2022 Xin | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×