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

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

相关推荐
说私域1 小时前
社群招募文案的核心构建要点与工具赋能路径——基于AI智能名片链动2+1模式商城小程序的实践研究
人工智能·小程序·私域运营
_ZeroKing3 小时前
自制智能门锁:NFC 刷卡 + 小程序远程开锁(完整实战记录)
嵌入式硬件·小程序·notepad++·arduino
郑州光合科技余经理3 小时前
可独立部署的Java同城O2O系统架构:技术落地
java·开发语言·前端·后端·小程序·系统架构·uni-app
雪芽蓝域zzs3 小时前
uniapp 取消滚动条
uni-app
千寻技术帮4 小时前
10386_基于SpringBoot的外卖点餐管理系统
java·spring boot·vue·外卖点餐
阿斌_bingyu7095 小时前
眼镜店AR在线试戴小程序技术解决方案
小程序·ar
2401_865854885 小时前
Uniapp和Flutter哪个更适合企业级开发?
flutter·uni-app
雪芽蓝域zzs5 小时前
uniapp 省市区三级联动
前端·javascript·uni-app
总爱写点小BUG5 小时前
UniApp 图标方案终极排坑:告别 FontClass,拥抱真 SVG 组件化
前端框架·uni-app
计算机毕设指导65 小时前
基于微信小程序的智能停车场管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea