当前位置: 首页 > news >正文

跑马灯组件 Vue2/Vue3/uni-app/微信小程序

 Vue2/Vue3/uni-app/微信小程序源代码资源地址:https://download.csdn.net/download/qiziyiming/92100217

JS动画实现跑马灯组件

css环境:unocss

动画时间更具文本或者插槽跨度自适应

<template><div :class="absolute ? 'absolute w-full h-full top-0 left-0 flex items-center' : ''"><div class="overflow-hidden max-w-full w-full"><a-tooltip class="max-w-full w-full" :placement="placement || 'bottom'"><template #title>{{ value }}</template><div ref="view" class="max-w-full w-full text no-scrollbar"><template v-if="$slots.default"><div ref="text" class="text min-w-full text-center"><slot></slot></div></template><div v-else ref="text" class="text">{{ value }}</div></div></a-tooltip></div></div>
</template><script lang="ts" setup>
import { defineProps, onMounted, ref, watch, onBeforeUnmount, nextTick } from 'vue';
interface Props {value?: string;show?: boolean;absolute?: boolean;duration?: number;placement?: string;
}
const props = defineProps<Props>();
const view = ref<HTMLElement | null>();
const text = ref<HTMLElement | null>();
watch(() => [props.value, props.show],() => {pause();nextTick(() => {initil(view.value as HTMLElement, text.value as HTMLElement);});}
);
onMounted(() => {nextTick(() => {initil(view.value as HTMLElement, text.value as HTMLElement);});
});
const animation = ref<Animation | null>(null);
onBeforeUnmount(() => {pause();
});
function pause() {if (animation.value) {animation.value.pause();animation.value.cancel();}
}
function initil(element: HTMLElement, text: HTMLElement) {pause();const scrollWidth = element.scrollWidth - element.clientWidth;if (scrollWidth > 0) {// node.style.transform = ''const _animation = text.animate([{ transform: 'translateX(0)' }, { transform: `translateX(-${scrollWidth + element.clientWidth + 10}px)` }], {delay: 3000,duration: ((element.scrollWidth / element.clientWidth) * (props.duration || 6) + 2) * 1000,// iterations: Infinity,// direction: 'infinite'});_animation.onfinish = () => {// _animation.pause();setTimeout(() => {_animation.play();}, 2000); // 暂停1秒};animation.value = _animation;// 冒泡型事件element.addEventListener('mouseover', () => {if (animation.value) {animation.value.pause();}});element.addEventListener('mouseout', () => {if (animation.value) {animation.value.play();}});}
}
</script><style lang="less">
.text {word-wrap: normal;text-wrap: normal;white-space: nowrap;
}.no-scrollbar {-ms-overflow-style: none;/* IE和Edge */scrollbar-width: none;/* Firefox */
}.no-scrollbar::-webkit-scrollbar {display: none;/* Chrome, Safari和Opera */
}
</style>

如果跑马灯没效果可以使用相对定位绝对定位设置组件宽度可以解决

http://www.dtcms.com/a/465125.html

相关文章:

  • 网络攻防实战:如何防御DDoS攻击
  • 能力(5)
  • 多模态医疗大模型Python编程合规前置化与智能体持续学习研究(下)
  • wordpress网站不显示系列秦皇岛网站制作与网站建设
  • 【2026计算机毕业设计】基于Springboot的广西美食宣传系统
  • Instagram投放转化率还能再提升!
  • Shell 脚本核心语法与企业实战案例
  • 学习爬虫第三天:数据提取
  • LightGBM评估指标中至关重要的参数【average】介绍
  • 基于tcl脚本构建Xilinx Vivado工程
  • 从3C电子到半导体封装,微型导轨具备哪些优势?
  • TCP中的流量控制
  • 专业建站推广网络公司网站建设和维护实训
  • AMD发布专为工业计算与自动化平台打造的锐龙嵌入式9000系列处理器
  • 短视频矩阵系统哪个好用?2025最新评测与推荐|小麦矩阵系统
  • 代理IP+账号矩阵:Cliproxy与TGX Account如何赋能品牌全球化表达?
  • 张量、向量与矩阵:多维世界的数据密码
  • 前端框架深度解析:Angular 从架构到实战,掌握企业级开发标准
  • 廊坊做网站教程泉州网站建设技术支持
  • 安全月报 | 傲盾DDoS攻击防御2025年9月简报
  • 有哪些做品牌特卖的网站做网页专题 应该关注哪些网站
  • 探索MySQL8.0隐藏特性窗口函数如何提升数据分析效率
  • 对于生物样本库的温湿度监控是如何实现对数据进行历史数据分析的呢?
  • 深入解析 Amazon Athena:云上高效数据分析的关键引擎
  • [SQL]如何使用窗口函数提升数据分析效率实战案例解析
  • Centos 7 | 定时运行 gzip 进程导致 CPU 过高,但无法确定系统自动运行 gzip 的原因 排查思路
  • Python爬虫实战:获取证监会外国投资机构信息及数据分析
  • seo网站推广费用装饰公司看的设计网站
  • 全栈开发杂谈————JAVA微服务全套技术栈详解
  • 微服务——SpringBoot使用归纳——Spring Boot中使用拦截器——拦截器的快速使用