前端实现跑马灯

<!--
 * @Author: wangrui
 * @Description: 跑马灯
 * @Date: 2024-11-08 11:45:17
 * @LastEditors: Do not edit
-->
<script setup lang="ts">
import { ref, watchEffect,withDefaults } from 'vue';
const props = withDefaults(defineProps<{
    notice: string[];
    duration?: number; 
    marqueeWrapperStyle?:Record<string, string>;
    marqueeItemStyle?:Record<string, string>;
}>(), {
    marqueeWrapperStyle: () => ({}),
    marqueeItemStyle: () => ({}),
    duration: 20
});

const MarqueeItemNameArr = ref(['origin', 'copy']);
const speed = ref(2);

const initSpeed = () => {
  if (!props.notice.length) {
    return false
 };
  const str = props.notice.join('').length;
  speed.value = (str / 40) * props.duration;
};

watchEffect(() => {
  if (props.notice.length) {
    initSpeed();
  }
});
</script>

<template>
    <div class="marquee" :style="marqueeWrapperStyle">
      <ol
        class="marquee-wrapper"
        :style="{
          animationDuration: `${speed}s`,
          WebkitAnimationDuration: `${speed}s`,
        }"
      >
        <li
          v-for="name in MarqueeItemNameArr"
          :key="`item-${name}`"
          class="marquee-item"
        >
          <div
            v-for="(item, index) in notice"
            :key="`item-${name}-${index}`"
            class="item"
            :style="marqueeItemStyle"
          >
            {{ item }}
          </div>
        </li>
      </ol>
    </div>
  </template>
<style scoped lang="scss">
ol,
li {
  padding: 0;
  margin: 0;
  list-style: none;
}
.marquee {
  overflow: hidden;
  width: 100%;
  display: flex;
  align-items: center;
  -webkit-mask-image: 
    linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1) 30%, rgba(0, 0, 0, 1) 70%, rgba(0, 0, 0, 0)),
    linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1) 30%, rgba(0, 0, 0, 1) 70%, rgba(0, 0, 0, 0));
  -webkit-mask-size: 100% 100%;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: left, right;
  mask-image: 
    linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1) 30%, rgba(0, 0, 0, 1) 70%, rgba(0, 0, 0, 0)),
    linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1) 30%, rgba(0, 0, 0, 1) 70%, rgba(0, 0, 0, 0));
  mask-size: 100% 100%;
  mask-repeat: no-repeat;
  mask-position: left, right;
}
.marquee-wrapper {
  height: inherit;
  margin: 0;
  padding: 0;
  will-change: transform;
  transform: translateZ(0);
  white-space: nowrap;
  -webkit-animation: marquee-ani linear infinite;
  animation: marquee-ani linear infinite;
}

.marquee-item {
  height: inherit;
  display: inline-block;
}

.item {
  margin-right: 40px;
  display: inline-block;
  font-size: 32px;
  color: #fff;
}
@-webkit-keyframes marquee-ani {
  0% {
    -webkit-transform: translateX(0);
    transform: translateX(0);
  }

  100% {
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
  }
}

@keyframes marquee-ani {
  0% {
    transform: translateX(0);
  }

  100% {
    transform: translateX(-50%);
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值