跑马灯组件 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 小时前
基于Vue3与TypeScript的后台管理系统设计与实现
前端·javascript·typescript·vue·毕业设计·源代码管理
计算机程序设计小李同学2 小时前
婚纱摄影集成管理系统小程序
java·vue.js·spring boot·后端·微信小程序·小程序
callJJ5 小时前
MCP配置与实战:深入理解现代开发工具链
javascript·node.js·vue·mcp·windsurf
幽络源小助理6 小时前
SpringBoot+小程序高校素拓分管理系统源码 – 幽络源免费分享
spring boot·后端·小程序
Mr -老鬼6 小时前
移动端跨平台适配技术框架:从发展到展望
android·ios·小程序·uni-app
内存不泄露7 小时前
棋牌预约小程序系统论文
小程序
shejizuopin8 小时前
基于JavaSSM+MySQL的实验室考勤管理系统设计与实现
java·mysql·vue·毕业设计·论文·springboot·实验室考勤管理系统设计与实现
winfredzhang8 小时前
从零构建:基于 Node.js 的全栈视频资料管理系统开发实录
css·node.js·html·音视频·js·收藏,搜索,缩略图
一颗小青松9 小时前
uniapp app端显示天气详情
uni-app
计算机徐师兄9 小时前
Java基于微信小程序的食堂线上预约点餐系统【附源码、文档说明】
java·微信小程序·食堂线上预约点餐系统小程序·食堂线上预约点餐微信小程序·java食堂线上预约点餐小程序·食堂线上预约点餐小程序·食堂线上预约点餐系统微信小程序