使用CSS的background-clip: text与linear-gradient结合animation实现文字颜色渐变动画,通过改变background-position使渐变背景流动,配合-webkit-text-fill-color: transparent让文字呈现动态渐变色效。
想让文字颜色在动画过程中实现渐变效果,可以结合 CSS 的 animation 与 background-clip: text 配合 linear-gradient 背景来实现。因为直接对 color
使用渐变是不支持的,但通过背景绘制再“裁剪到文字”,就能达到视觉上的文字渐变动画效果。
核心思路:给文字设置一个渐变背景,然后用 background-clip: text
让背景只显示在文字区域内,再配合 -webkit-text-fill-color: transparent
把文字本身颜色设为透明。
.gradient-text { font-size: 48px; font-weight: bold; background-image: linear-gradient(45deg, #ff7a00, #ff0080, #c800ff); background-size: 200% 200%; -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
通过改变 background-position
来实现渐变流动的效果,让它看起来像颜色在动态过渡。
@keyframes gradient-shift { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } .gradient-text { animation: gradient-shift 3s ease-in-out infinite; }
把上面所有部分组合起来:
立即学习“前端免费学习笔记(深入)”;
<style> .animated-gradient-text { font-size: 56px; font-weight: bold; background-image: linear-gradient(45deg, #ff7a00, #ff0080, #c800ff, #7d00ff); background-size: 200% 200%; -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; animation: gradient-shift 4s ease-in-out infinite; } @keyframes gradient-shift { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } </style> <div class="animated-gradient-text">渐变动效文字</div>
transform
或 opacity
动画更高效,但这里动画的是 background-position,虽然稍重但仍可接受。以上就是css animation与color文字渐变结合的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号