vue3-seamless-scroll替代源码

旧项目用的 vue3-seamless-scroll 怎么写都不生效,一看源码两年没更新了,不想调试,还是自己写吧,再不济问问GPT都会更快一点
 

效果图

scroll.vue

<template>
  <div class="scroll-container" ref="scrollContainerRef">
    <div class="scroll-content" ref="scrollContentRef">
      <div
        v-for="(item, index) in items"
        :key="index"
        class="scroll-item"
        :style="{ color: fontColor }"
      >
        {{ item.Content }}
      </div>
      <div
        v-for="(item, index) in items"
        :key="'clone-' + index"
        class="scroll-item"
        :style="{ color: fontColor }"
      >
        {{ item.Content }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";

const props = defineProps({
  items: {
    type: Array,
    default: [],
  },
});

const fontColor = ref("#ffffff");
const colors = ref([
  "#00FFD0",
  "#FF8A22",
  "#FFFC28",
  "#FF6B5C",
  "#FFA24F",
  "#AAFFA8",
]);
const intervalId = ref();
const getRandomColor = () => {
  const randomIndex = Math.floor(Math.random() * colors.value.length);
  return colors.value[randomIndex];
};

const changeColor = () => {
  fontColor.value = getRandomColor();
};

const scrollContainerRef = ref(null);
const scrollContentRef = ref(null);

const updateDimensions = () => {
  const scrollContainerWidth = scrollContainerRef.value.offsetWidth;
  const scrollContentWidth = scrollContentRef.value.scrollWidth / 2;
  const scrollContentElement = scrollContentRef.value;

  scrollContentElement.style.setProperty(
    "--scroll-content-width",
    `${scrollContentWidth}px`
  );
  scrollContentElement.style.setProperty(
    "--scroll-container-width",
    `${scrollContainerWidth}px` 
  );
};

const startAnimation = () => {
  const scrollContentElement = scrollContentRef.value;
  scrollContentElement.classList.remove('paused');
  scrollContentElement.style.animation = 'none';
  scrollContentElement.offsetHeight; // Trigger reflow
  scrollContentElement.style.animation = '';
  scrollContentElement.classList.add('running');
};

onMounted(() => {
  updateDimensions();
  window.addEventListener("resize", updateDimensions);
  changeColor();
  intervalId.value = setInterval(changeColor, 1000);
  startAnimation()
});

onUnmounted(() => {
  window.removeEventListener("resize", updateDimensions);
  clearInterval(intervalId.value);
});
</script>

<style lang="scss" scoped>
.scroll-container {
  overflow: hidden;
  white-space: nowrap;
  width: 100%;
}

.scroll-content {
  display: flex;
  animation: scroll 20s linear infinite;
}

.scroll-item {
  display: inline-block;
  font-size: 24px;
  height: 29px;
  line-height: 29px;
  padding-right: 20px;
  transition: color 1s;
}

@keyframes scroll {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(calc(-1 * var(--scroll-content-width)));
  }
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值