跑马灯组件 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>

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

相关推荐
无衣 秦风4 小时前
vue3+hubuilderX开发微信小程序使用elliptic生成ECDH密钥对遇到的问题
微信小程序·小程序
游戏开发爱好者84 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_916008894 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
苏打水com5 小时前
前端框架深度解析:Vue.js 3 从 Composition API 到生态升级,解锁企业级开发新能力
vue
流***陌5 小时前
陪诊就医小程序中健康档案的精细化管理设计方案
小程序
明天你好2675 小时前
如何做一个花店小程序,搭建一个小程序多少钱
微信小程序·小程序·模拟退火算法
2501_915106326 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
江城开朗的豌豆6 小时前
小程序登录不迷路:一篇文章搞定用户身份验证
前端·javascript·微信小程序
2501_915921436 小时前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式