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

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

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

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>

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

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

相关推荐
用户550849290256几秒前
CSS布局实战:从Flex到Grid的完整避坑指南
前端
用户5508492902561 分钟前
前端接口请求层怎么封装?一个可落地的请求方案
前端
MetWeave15 分钟前
跟着一条 SPECI 走流水线:从电报到界面
前端
Helix25032 分钟前
Chrome 开发者工具进阶:长截图、网络调试与性能分析
前端·chrome·chrome devtools·使用技巧·性能分析·开发者工具·长截图
liangshanbo12151 小时前
面试题:Webpack 的 publicPath 有什么作用?
前端·webpack·node.js
mjhcsp1 小时前
DeepSeek V4.1 Flash 批量处理效能实测
java·前端·数据库
CHEEVEN_QY2 小时前
新能源汽车电池托盘FSW量产的工艺稳定性控制
java·前端·汽车
Alice-YUE2 小时前
前端 × AI:从 Cursor 到 Transformer,一份完整认知路径
前端·人工智能·transformer·ai编程·前端开发·cursor
Code_Solitude2 小时前
C语言:关于二维数组的作业总结
java·c语言·前端
acd120093 小时前
前端随笔:数据明明变了,Vue 页面就是不更新
前端·javascript·vue.js