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

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

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

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>

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

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

相关推荐
Net蚂蚁代码36 分钟前
Angular入门的环境准备步骤工作
前端·javascript·angular.js
小着3 小时前
vue项目页面最底部出现乱码
前端·javascript·vue.js·前端框架
lichenyang4536 小时前
React ajax中的跨域以及代理服务器
前端·react.js·ajax
呆呆的小草6 小时前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript
一 乐7 小时前
民宿|基于java的民宿推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·源码
testleaf7 小时前
前端面经整理【1】
前端·面试
好了来看下一题7 小时前
使用 React+Vite+Electron 搭建桌面应用
前端·react.js·electron
啃火龙果的兔子7 小时前
前端八股文-react篇
前端·react.js·前端框架
小前端大牛马7 小时前
react中hook和高阶组件的选型
前端·javascript·vue.js
刺客-Andy7 小时前
React第六十二节 Router中 createStaticRouter 的使用详解
前端·javascript·react.js