vue跑马灯效果
功能:根本文字时长设置滚动时长,鼠标移上去停止滚动,把鼠标移开继续滚动
我这个是滚动2遍则隐藏,如果需要一直滚动的,把定时关掉就了。
<template> <div id="app"> <div class="marquee"> <div class="text" :class="{ 'animate-once': animateOnce }" :style="{ animationDuration: animationDuration + 's' }"> 这是跑马灯文字,滚动两遍后消失。 </div> </div> </div> </template> <script> export default { data() { return { animateOnce: false, animationDuration: 30 // 默认动画持续时间 }; }, mounted() { // 动态计算动画持续时间 const textLength = this.$el.querySelector('.text').textContent.length; this.animationDuration = Math.max(10, textLength * 0.5); // 根据文字长度调整动画时间,最短10秒 // 使用setTimeout模拟动画执行完毕后的操作 setTimeout(() => { this.animateOnce = true; // 延迟2次动画时长后移除动画类名,文字消失 setTimeout(() => { this.animateOnce = false; }, 2 * this.animationDuration * 1000); // 动态调整延迟时间 }, 1000); // 1000ms是给文字一点时间进行第一次滚动 } }; </script> <style> .marquee { white-space: nowrap; overflow: hidden; box-sizing: border-box; } .text { display: inline-block; padding-left: 100%; //文字右移的距离,可根据自己的调节 animation: scroll-once linear infinite; } .animate-once .text { animation: scroll-once linear 2; } @keyframes scroll-once { 0% { transform: translateX(0); } 100% { transform: translateX(-100%); } } </style>
分类:
vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了