通过一个组件实现了文字流光效果

最近看到百度首页有一个文字流光的效果,挺有意思的,在项目中可以找一个让人"眼前一黑"的地方加上这个效果。

保准让人"眼前一黑"。哈哈,效果虽然很简单,但是颇有零几年网页的"赶脚"~

html 复制代码
<template>
    <div class="shine-font-container">
        <div 
            class="shine-font" 
            :style="{ 
                backgroundImage: gradientStyle, 
                animationDuration: animationDuration + 's', 
                animationIterationCount: isAnimationLoop ? 'infinite' : 1 
            }"
        >
            {{ text }}
        </div>
    </div>
</template>

<script>
export default {
    name: 'ShineFont',
    props: {
        text: {
            type: String,
            default: '默认文本'
        },
        gradientColor: {
            type: Array,
            default: () => ['#ff1E0033', '#ff1e00', '#335bff', '#335BFF33']
        },
        animationDuration: {
            type: Number,
            default: 5
        },
        isAnimationLoop: {
            type: Boolean,
            default: true
        }
    },
    computed: {
        gradientStyle() {
            return `linear-gradient(90deg, ${this.gradientColor.join(', ')})`;
        }
    }
}
</script>

<style scoped>
.shine-font-container .shine-font {
    background-size: 120% 100%;
    background-position: -753em 0;
    background-repeat: no-repeat;
    animation: shine 1s linear;
    font-size: 16px;
    font-weight: 400;
    line-height: 25px;
    height: 24px;
    letter-spacing: .06em;
    text-align: left;
    background: #000;
    color: transparent;
    background-clip: text;
    -webkit-background-clip: text;
    display: flex;
}

@keyframes shine {
    0% {
        background-position: -150px 0;
    }
    100% {
        background-position: 150px 0;
    }
}
</style>

其实效果比较简单,就是给背景一个渐变色,给文字一个透明色。

通过背景不断地挪动位置实现文字"流光"的效果。

相关推荐
小江的记录本2 分钟前
【JEECG Boot】 JEECG Boot 数据字典管理——六大核心功能(内含:《JEECG Boot 数据字典开发速查清单》)
java·前端·数据库·spring boot·后端·spring·mybatis
小江的记录本2 分钟前
【JEECG Boot】 JEECG Boot——Online表单 系统性知识体系全解
java·前端·spring boot·后端·spring·低代码·mybatis
John_ToDebug3 分钟前
Chromium 页面类型与 IPC 通信机制深度解析
前端·c++·chrome
Fanfffff7204 分钟前
前端进阶:从请求竞态到并发控制(系统学习笔记)
前端·笔记·学习
大、男人5 分钟前
edge浏览器打开baidu.com很慢,我是如何解决的
前端·edge
吴声子夜歌6 分钟前
ES6——函数的扩展详解
前端·ecmascript·es6
有趣的老凌7 分钟前
一篇文章带你了解 Agent Skills —— 告别AI“失控”
前端·agent·claude
~ rainbow~10 分钟前
前端转型全栈(二)——NestJS 入门指南:从 Angular 开发者视角理解后端架构
前端·javascript·angular.js
恋猫de小郭14 分钟前
AGP 9.2 开始,Android 上协程启动和取消速度提升两倍
android·前端·flutter