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

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

JS动画实现跑马灯组件

css环境:unocss

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

html 复制代码
<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>

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

相关推荐
椰羊~王小美2 小时前
前端js用法:val ?? 0 和 val || 0
js
iOS阿玮5 小时前
1V1 社交精准收割 3.6 亿!40 款马甲包 + 国内社交难度堪比史诗级!
uni-app·app·apple
是梦终空9 小时前
JAVA毕业设计259—基于Java+Springboot+vue3工单管理系统的设计与实现(源代码+数据库+开题报告)
java·spring boot·vue·毕业设计·课程设计·工单管理系统·源代码
脾气有点小暴10 小时前
uniapp开发APP 内嵌外部 HTTPS 链接的实现方案
vue.js·uni-app
@AfeiyuO12 小时前
封装表格操作列按钮
vue
千寻技术帮14 小时前
10382_基于Springboot的高校排课管理系统
mysql·vue·毕设·spingboot·高校排课
IT教程资源C15 小时前
(N_115)基于springboot,vue教务管理系统
mysql·vue·前后端分离·springboot教务系统
沧澜sincerely15 小时前
WebSocket 实时聊天功能
网络·websocket·vue·springboot
苏打水com16 小时前
第二十篇:Day58-60 前端性能优化进阶——从“能用”到“好用”(对标职场“体验优化”需求)
前端·css·vue·html·js
Jeking21716 小时前
进阶流程图绘制工具 Unione Flow Editor-- 直击行业痛点:高扩展性解决方案解析
vue·流程图·workflow·unione flow·flow editor·unione cloud