Vue 跑马灯横向滚动(单文本)

完整代码如下

<template>
  <div
    class="notice-bar"
    @click="tipClick"
    @mouseenter="pauseAnimation"
    @mouseleave="resumeAnimation"
  >
    <div class="notice-bar__icon">
      温馨提示:
    </div>
    <div class="notice-bar__wrap">
      <div class="notice-bar__content" :style="{ animationDuration: animationDuration, animationPlayState: animationPlayState }">
        {{ text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NoticeBar',
  props: {
    text: {
      type: String,
      default: ''
    },
    animationDuration: {
      type: String,
      default: '10s'
    }
  },
  data() {
    return {
      animationPlayState: 'running' // 初始状态为运行
    }
  },
  methods: {
    tipClick() {
      this.$emit('click')
    },
    pauseAnimation() {
      this.animationPlayState = 'paused' // 鼠标移入时暂停动画
    },
    resumeAnimation() {
      this.animationPlayState = 'running' // 鼠标移出时恢复动画
    }
  }
}
</script>

<style scoped>
.notice-bar {
  cursor: pointer;
  position: relative;
  width: 100%;
  padding: 0;
  font-weight: 400;
  display: flex;
  height: 100%;
  align-items: center;
}

.notice-bar__icon {
  color: #fff;
  font-size: 13px;
  width: 70px;
}

.notice-bar__wrap {
  color: #fff;
  font-size: 13px;
  position: relative;
  display: flex;
  flex: 1;
  align-items: center;
  overflow: hidden;
}

.notice-bar__content {
  white-space: nowrap;
  animation: marquee linear infinite;
}

@keyframes marquee {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(-100%);
  }
}
</style>

实现 Vue 跑马灯横向滚动并且无缝连接,可以采用以下步骤: 1. 在 Vue 组件中编写 HTML 和 CSS,用于显示滚动区域和滚动内容。 2. 在 Vue 组件的 data 中定义一个数组,用于存储滚动内容的数据。 3. 在 Vue 组件的 mounted 钩子中,使用定时器和 CSS3 动画实现滚动效果。 4. 在滚动到最后一个元素时,将第一个元素移动到最后一个元素的后面,从而实现无缝连接的效果。 下面是一个简的示例代码,供你参考: ```html <template> <div class="scroll-container"> <div class="scroll-content"> <div v-for="(item, index) in items" :key="index" class="scroll-item"> {{ item }} </div> </div> </div> </template> <script> export default { data() { return { items: ['item1', 'item2', 'item3', 'item4', 'item5'] }; }, mounted() { const container = this.$el.querySelector('.scroll-container'); const content = this.$el.querySelector('.scroll-content'); const items = this.$el.querySelectorAll('.scroll-item'); let index = 0; setInterval(() => { const currentItem = items[index]; const nextItem = items[(index + 1) % items.length]; content.style.transform = `translateX(-${currentItem.offsetLeft}px)`; container.style.width = `${currentItem.offsetWidth}px`; setTimeout(() => { currentItem.classList.add('scroll-item-hidden'); nextItem.classList.remove('scroll-item-hidden'); if (index === items.length - 1) { items[0].classList.remove('scroll-item-hidden'); } index = (index + 1) % items.length; }, 500); }, 3000); } }; </script> <style> .scroll-container { overflow: hidden; } .scroll-content { display: flex; transition: transform 0.5s ease; } .scroll-item { flex: none; margin-right: 20px; padding: 10px; background-color: #eee; } .scroll-item-hidden { display: none; } </style> ``` 在这个示例中,我们定义了一个包含 5 个元素的数组 items,用于存储滚动内容的数据。在组件的 mounted 钩子中,我们使用了 setInterval 和 setTimeout 方法实现滚动和无缝连接的效果。CSS 方面,我们使用了 flex 布局和 transform 属性实现滚动的效果,使用了 display 属性实现了无缝连接的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值