vue组件走马灯_Vue-component | 文字走马灯组件

这篇博客介绍了如何在Vue中创建一个文字走马灯组件,通过接收props传递的数据实现内容的循环滚动展示,并利用Vue的过渡动画增强视觉效果。博主分享了关键代码段,包括设置定时器和处理组件传值,同时承诺会继续更新更多实用组件。
摘要由CSDN通过智能技术生成

日常开发中积累了不少可能对一些开发有帮助的简单易用的组件,好记性不如烂笔头,想对过去的一些零零乱乱的东西做一些总结,反省自己的同时也便于思考一些更优的方法,提升下开发思维😉😉😉。

代码传送门(😃感觉有作用的的同学帮忙点下❤️️)

效果截图

先上效果图,滚动展示不同数据。

785134d9a513

Carousel

组件结构

需要传递一个指定格式的数组作为内容

核心代码

export default {

name: 'j-carousel',

components: {},

data () {

return {

number: 0,

timer: 0

}

},

props: {

dataList: Array

},

computed: {

text () {

return {

id: this.number,

val: this.dataList[this.number]

}

}

},

watch: {},

methods: {

startMove () {

// eslint-disable-next-line

this.timer = setTimeout(() => {

if (this.number === this.dataList.length - 1) {

this.number = 0

} else {

this.number += 1

}

this.startMove()

}, 4000) // 滚动不需要停顿则将2000改成动画持续时间

}

},

mounted () {

this.startMove()

},

beforeDestroy () {

clearTimeout(this.timer)

}

}

关键点

处理自定义组件,一定要对vue中的传值比较清晰了解,这里就不一一列举。在这里主要使用的一些技术包括:

技术

概述

备注

props传值

父级传子级

/

setTimeout

开启计时,文字循环展示

/

Vue过渡动画

.slide-enter-active,.slide-leave-active,.slide-enter,.slide-leave-to

/

后续会持续更新其他一些有用的组件提供参考...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Vue 组件实现文字横向无缝走马灯效果的实例代码: ```vue <template> <div class="marquee"> <div class="marquee-inner" ref="marquee"> <slot></slot> <slot></slot> </div> </div> </template> <script> export default { name: 'Marquee', data() { return { animationId: null, speed: 1, offset: 0, innerWidth: 0, marqueeWidth: 0 } }, mounted() { this.startAnimation() }, beforeDestroy() { this.stopAnimation() }, methods: { startAnimation() { this.animationId = requestAnimationFrame(this.animate.bind(this)) }, stopAnimation() { cancelAnimationFrame(this.animationId) }, animate() { this.offset -= this.speed if (this.offset < -this.marqueeWidth) { this.offset = 0 } this.$refs.marquee.style.transform = `translateX(${this.offset}px)` this.animationId = requestAnimationFrame(this.animate.bind(this)) }, updateDimensions() { this.innerWidth = this.$refs.marquee.clientWidth this.marqueeWidth = this.$refs.marquee.scrollWidth } }, mounted() { window.addEventListener('resize', this.updateDimensions) this.updateDimensions() }, beforeDestroy() { window.removeEventListener('resize', this.updateDimensions) } } </script> <style scoped> .marquee { overflow: hidden; white-space: nowrap; } .marquee-inner { display: inline-block; animation: none; } </style> ``` 该组件包含一个名为 `Marquee` 的 Vue 组件,它具有以下功能: - 在组件内部包含一个具有 `marquee-inner` 类名的 div 元素,用于包含实际的滚动内容。 - 使用 `requestAnimationFrame` API 实现动画效果。 - 使用 `translateX` CSS 属性在水平方向上移动滚动内容。 - 监听窗口大小变化事件,并在更新时重新计算滚动内容的宽度。 使用该组件时,只需要需要滚动的内容放入组件的插槽中即可: ```vue <marquee> <span>这是滚动文字1</span> <span>这是滚动文字2</span> <span>这是滚动文字3</span> <span>这是滚动文字4</span> </marquee> ``` 你可以根据需要自定义组件的样式,例如设置滚动速度等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值