CSS实现好看的聚光灯效果

整体效果

使用 -webkit-background-clip 和  clip-path ,并配合 animation 属性,实现一个好看聚光灯效果。

可适用于页面加载或展示页面大标题内容。

核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

1
<div>spotlight</div>

主体部分一个 div 标签。

css 部分代码

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
.spotlight18{
  color#eaeaea;
  font-size40px;
  font-weight900;
  text-transformuppercase;
  positionrelative;
}
.spotlight18:before{
  widthinherit;
  heightinherit;
  contentattr(data-cont);
  clip-path: ellipse(32px 32px at 0 50%);
  colortransparent;
  background-imagelinear-gradient(90deg, #4158D0 0%#C850C0 30%#FFCC70 66%#56e28d 100%);
  -webkit-background-clip: text;
  positionabsolute;
  top0;
  left0;
  animation: spotlight18 8linear infinite;
}
@keyframes spotlight18{
  0%{
    clip-path: ellipse(32px 32px at 0 50%);
  }
  50%{
    clip-path: ellipse(32px 32px at 100% 50%);
  }
  100%{
    clip-path: ellipse(32px 32px at 0 50%);
  }
}

用 background-image 的  linear-gradient 拉出渐变背景,让文字颜色透明 color: transparent ,然后配合  -webkit-background-clip: text 给文字实现渐变效果,最后加上动画属性 animation 并设置 clip-path 参数就可以啦。

注意:这里使用的是 -webkit-background-clip ,而不是 background-clip 。

完整代码如下

html 页面

1
2
3
4
5
6
7
8
9
10
11
12
html>
 
   
     
     
    <title>聚光灯效果</title>
   
   
    <div>
      <div>spotlight</div>
    </div>
   

css 样式

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
/** style.css **/
.app{
  width100%;
  height100vh;
  background-color#ffffff;
  positionrelative;
  displayflex;
  justify-contentcenter;
  align-itemscenter;
}
.spotlight18{
  color#eaeaea;
  font-size40px;
  font-weight900;
  text-transformuppercase;
  positionrelative;
}
.spotlight18:before{
  widthinherit;
  heightinherit;
  contentattr(data-cont);
  colortransparent;
  background-imagelinear-gradient(90deg, #4158D0 0%#C850C0 30%#FFCC70 66%#56e28d 100%);
  -webkit-background-clip: text;
  positionabsolute;
  top0;
  left0;
  animation: spotlight18 8linear infinite;
}
@keyframes spotlight18{
  0%{
    clip-path: ellipse(32px 32px at 0 50%);
  }
  50%{
    clip-path: ellipse(32px 32px at 100% 50%);
  }
  100%{
    clip-path: ellipse(32px 32px at 0 50%);
  }
}

页面渲染效果

到此这篇关于CSS实现好看的聚光灯效果的文章就介绍到这了,更多相关CSS聚光灯内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章,希望大家以后多多支持IT俱乐部!

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

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

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

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

微信扫一扫关注我们

返回顶部