vue悬浮 内容滚动实现_vue 实现文字无缝滚动效果(从下往上滚动)

需求:对中奖用户进行滚动效果展示

这种效果需求还是蛮多的,想起之前用JQuery实现的一个无缝滚动( 缅怀过去),是通过jq的animate方法实现的,动画结束之后就将第一个元素appendTo追加到最后面,实现循环滚动特效,不得不感叹技术更新换代真的很快。

回到现在Vue的项目,本来想用插件,但是觉得这么一个小动画用插件有点太重了,还是自己写一个比较好。

实现原理

实现原理也比较简单

对整个列表实现上移动画

将列表的第一个数据移动到最后一个

因为vue是基于数据驱动的,所以,对我们开发者来说,直接操控数组,删除第一个数组数据,然后追加到数组后面就好了。

点此查看实现效果以及源码预览可以略过下面的内容

template 部分

  • {{item}}

script部分

export default {

name: "marquee-up",

data() {

return {

animateUp: false,

listData: ['12***ve 成功邀请12人 已获奖金60元', 'l***e 成功邀请5人 已获奖金40元', 'l***e 成功邀请1人 已获奖金5元'],

timer: null

}

},

mounted() {

this.timer = setInterval(this.scrollAnimate, 1500);

},

methods: {

scrollAnimate() {

this.animateUp = true

setTimeout(() => {

this.listData.push(this.listData[0])

this.listData.shift()

this.animateUp = false

}, 500)

}

},

destroyed() {

clearInterval(this.timer)

}

};

style 部分

.marquee-wrap {

width: 80%;

height: 40px;

border-radius: 20px;

background: rgba($color: #000000, $alpha: 0.6);

margin: 0 auto;

overflow: hidden;

.marquee-list {

li {

width: 100%;

height: 100%;

text-overflow: ellipsis;

overflow: hidden;

white-space: nowrap;

padding: 0 20px;

list-style: none;

line-height: 40px;

text-align: center;

color: #fff;

font-size: 18px;

font-weight: 400;

}

}

.animate-up {

transition: all 0.5s ease-in-out;

transform: translateY(-40px);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值