vue 跑马灯

import scroll from 'vue-seamless-scroll'

Vue.use(scroll)

具体的安装步骤就不说了,也是进行了单独的封装,直接上代码(没有具体写外部传入组件内部内容),有滚动并且划上去会停止

<template>
  <vue-seamless-scroll :data="newsList" :class-option="optionLeft" class="seamless-warp2">
   <span>{{newsList[0]}}</span> 
  </vue-seamless-scroll>
</template>

<script>
export default {
  data() {
    return {
      newsList: [  "我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯我是跑马灯" ]
    };
  },
  computed: {
    optionLeft() {
      return {
        direction: 2,
        limitMoveNum: 1
      };
    }
  }
};
</script>
<style lang="scss" scoped>

</style>

引用

<template>
  <div style="width: 100%; height: 100%">
      <common-scroll></common-scroll>
  </div>
</template>

<script>

import CommonScroll from "./components/commonScroll";
export default {
  data() {
    return {
     
    };
  },
  
  methods: {
   
  },
  mounted() {
  },
  components: {
    CommonScroll
  }
};
</script>

<style scoped>


</style>

 

### 实现 Vue 跑马灯组件的方法 在 Vue 中实现跑马灯效果可以通过多种方式实现,包括使用原生 JavaScript、CSS 动画以及 Vue 的过渡系统等。以下将介绍几种实现方法,并结合提供的代码片段说明如何创建一个可复用的跑马灯组件。 #### 方法一:基于 Vue 的定时器实现 使用 Vue 的 `data` 和 `methods` 来控制文本的滚动逻辑,通过 `setInterval` 实现文本的逐字符滚动效果。该方法适用于简单的文本滚动需求。 ```html <template> <div> <button @click="start" :disabled="isDisabled">开始滚动</button> <button @click="stop" :disabled="!isDisabled">停止</button> <div>{{ msg }}</div> </div> </template> <script> export default { data() { return { msg: "猥琐发育,别浪~~!", timer: null, isDisabled: false }; }, methods: { start() { this.isDisabled = !this.isDisabled; clearInterval(this.timer); this.timer = setInterval(() => { this.msg = this.msg.substring(1) + this.msg.substring(0, 1); }, 500); }, stop() { this.isDisabled = !this.isDisabled; clearInterval(this.timer); } } }; </script> ``` 此方法通过字符串截取并重新拼接的方式实现字符逐个滚动,适用于短文本滚动显示[^2]。 --- #### 方法二:基于 CSS 动画实现 使用 CSS 的 `transition` 和 `transform` 属性实现文本的平滑滚动动画,适用于长文本或需要更流畅滚动效果的场景。 ```html <template> <div class="marquee-container"> <div class="marquee-text" :style="{ left: currentPosition + 'px' }"> {{ text }} </div> </div> </template> <script> export default { props: { text: String, speed: { type: Number, default: 120 } }, data() { return { currentPosition: 0, textWidth: 0, timer: null }; }, mounted() { this.textWidth = this.$el.querySelector('.marquee-text').offsetWidth; this.start(); }, methods: { start() { this.timer = setInterval(() => { this.currentPosition -= 1; if (this.currentPosition < -this.textWidth) { this.currentPosition = 0; } }, this.speed); }, stop() { clearInterval(this.timer); } } }; </script> <style> .marquee-container { overflow: hidden; white-space: nowrap; position: relative; width: 100%; } .marquee-text { position: absolute; transition: left 0.5s linear; } </style> ``` 此方法通过控制 `left` 样式实现文本的持续滚动,并通过 `setInterval` 控制定速滚动。适用于长文本的无缝滚动[^3]。 --- #### 方法三:封装为组件并支持点击事件 将跑马灯封装为组件,支持点击事件传递和动态文本更新,适用于需要交互的场景。 ```html <template> <div class="marquee-box__container" @click="handleClick"> <div class="marquee-text" :style="{ left: currentPosition + 'px' }"> {{ text }} </div> </div> </template> <script> export default { props: { text: String, speed: { type: Number, default: 120 }, itemClick: Boolean }, data() { return { currentPosition: 0, textWidth: 0, timer: null }; }, mounted() { this.textWidth = this.$el.querySelector('.marquee-text').offsetWidth; this.start(); }, methods: { start() { this.timer = setInterval(() => { this.currentPosition -= 1; if (this.currentPosition < -this.textWidth) { this.currentPosition = 0; } }, this.speed); }, stop() { clearInterval(this.timer); }, handleClick(event) { if (this.itemClick) { this.$emit('itemClickEvent', event); } } } }; </script> ``` 该组件支持点击事件绑定,并通过 `@itemClickEvent` 向父组件传递点击事件,增强了交互性[^1]。 --- #### 方法四:基于 Vue 过渡组件实现 使用 `<transition>` 或 `<transition-group>` 实现文本的逐行滚动动画,适用于需要逐行滚动或分页显示的场景。 ```html <template> <transition name="slide"> <div class="marquee-line" :key="currentIndex"> {{ lines[currentIndex] }} </div> </transition> </template> <script> export default { props: { lines: Array, interval: { type: Number, default: 3000 } }, data() { return { currentIndex: 0, timer: null }; }, mounted() { this.timer = setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.lines.length; }, this.interval); }, beforeDestroy() { clearInterval(this.timer); } }; </script> <style> .slide-enter-active, .slide-leave-active { transition: all 1s ease; } .slide-enter { transform: translateY(100%); opacity: 0; } .slide-leave-to { transform: translateY(-100%); opacity: 0; } </style> ``` 此方法适用于逐行显示的跑马灯场景,通过 Vue 的过渡系统实现动画效果。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值