效果图如下:
主要思路
使用::after创建与原文本同区域位置的文字
clip-path ellipse 裁剪创建元素的可显示区域
动画执行裁剪区域
background-clip:text 以区块内的文字作为裁剪区域向
外裁剪,文字的背景即为区块的背景,文字之外的区
域都将被裁剪掉,将文字设置为 transparent
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>focus</title>
<style>
html {
font-size: 15px;
}
body {
background-color: #222;
display: flex;
justify-content: center;
align-items: center;
min-height: 90vh;
}
h1 {
color: #333;
font-family: Helvetica;
margin: 0;
padding: 0;
font-size: 8rem;
letter-spacing: -0.3rem;
position: relative;
}
h1::after {
content: attr(data-spotlight);
color: transparent;
position: absolute;
top: 0;
left: 0;
-webkit-clip-path: ellipse(100px 100px at 0% 50%);
clip-path: ellipse(100px 100px at 0% 50%);
animation: spotlight 5s infinite;
background-image: url(https://images.unsplash.com/photo-1579547621869-0ddb5f237392?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60);
background-size: 150%;
background-position: center center;
-webkit-background-clip: text;
background-clip: text;
}
@keyframes spotlight {
0% {
-webkit-clip-path: ellipse(100px 100px at 0% 50%);
clip-path: ellipse(100px 100px at 0% 50%);
}
50% {
-webkit-clip-path: ellipse(100px 100px at 100% 50%);
clip-path: ellipse(100px 100px at 100% 50%);
}
100% {
-webkit-clip-path: ellipse(100px 100px at 0% 50%);
clip-path: ellipse(100px 100px at 0% 50%);
}
}
</style>
</head>
<body>
<h1 data-spotlight="codingchangelife">codingchangelife</h1>
</body>
</html>
|