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

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

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

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>

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

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

相关推荐
zhanlang02171 分钟前
HTML 零基础入门到企业实战全套教程【第五章 5.5 综合案例:图文影音展示页面开发】
前端·html
zhanlang021728 分钟前
HTML 零基础入门到企业实战全套教程【连载 5.3】audio、video 音频视频标签|兼容格式、自动播放踩坑、企业规范
前端·html
qq_406981031 小时前
c++ lamda(三)Lambda表达式
java·前端·c++
zhanlang02171 小时前
HTML 零基础入门到企业实战【第五章 5.4】内嵌网页 iframe 标签:嵌入网页、视频、地图实战
前端·html·音视频
2分钟速写快排9 小时前
孔乙己的茴字有多少种写法?|AI 对话实时通信的三种实现
前端·javascript·github
Penge66611 小时前
JavaScript:BOM 详解
前端
Penge66611 小时前
Vite vs Webpack:前端构建工具原理与实战
前端
Penge66611 小时前
前端包管理工具:npm、Yarn、pnpm 快速入门
前端
Penge66612 小时前
Sass 了解
前端
Penge66612 小时前
Webpack 打包从 0 到 1 实战笔记
前端