vue 消息滚动通知栏

本文介绍了一种使用Vue.js创建滚动新闻组件的方法,通过定时更新DOM元素展示新闻列表,实现了从下往上的滚动效果。文章详细解释了如何利用Vue的数据绑定特性,结合HTML和CSS实现流畅的动画过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从下往上跑
在这里插入图片描述

1

<div class="news flex-bet">
	<div class="img flex-bet">
		<img class="nioce" src="../assets/images/home03.png" />
		<img class="icon" src="../assets/images/home04.png" />
	</div>
	<div class="news-cot">
		<ul :class="{marquee_top:animate}">
			<li class="h" v-for="(item,index) in news">{{item.name}}</li>
		</ul>
	</div>
</div>

2

export default {
		name: "Home",
		data() {
			return{
				animate: false,
		     	news: [
		     		{
		     			name: '对接口白色的可看出'
	     			},{
		     			name: '就的女会计师你开饭店'
	     			},{
		     			name: '机不可失打开的方式'
	     			},{
		     			name: '接口部分都看见你发靠的就是南方'
	     			},{
		     			name: '开办费离开你浪费'
	     			},{
		     			name: '举报你放假你发个技能奉公守法'
	     			}
		     		
		     	]
			}
		},
		methods: {
			showMarquee: function() {
		      this.animate = true;
		
		      setTimeout(() => {
		        this.news.push(this.news[0]);
		
		        this.news.shift();
		
		        this.animate = false;
		      }, 500);
		    }
		}
}

3

.marquee_top {transition: all 0.5s;margin-top: -45px;}
你可以使用Vue 3和Element Plus来实现一个滚动通知栏。以下是一个简单的示例: 首先,安装Element Plus和Vue Router(如果还没有安装): ```bash npm install element-plus vue-router ``` 然后,在你的Vue文件中,引入所需的组件和样式: ```vue <template> <div class="scroll-notice"> <el-scrollbar ref="scrollbar" :wrap-style="{ height: '40px' }"> <el-scrollbar-wrap> <div class="notice-content" :style="{ top: top + 'px' }"> <!-- 这里放置通知内容 --> <div v-for="(notice, index) in notices" :key="index" class="notice-item"> {{ notice }} </div> </div> </el-scrollbar-wrap> </el-scrollbar> </div> </template> <script> import { ref, reactive, watch } from 'vue'; export default { name: 'ScrollNotice', data() { return { top: 0, notices: [ '通知1', '通知2', '通知3', // 更多通知... ], }; }, mounted() { this.startScroll(); }, methods: { startScroll() { const scrollHeight = this.$refs.scrollbar.$el.clientHeight; const contentHeight = this.$refs.scrollbar.scrollContent.offsetHeight; if (contentHeight > scrollHeight) { // 开始滚动 const updateTop = () => { this.top -= 1; if (this.top <= -contentHeight) { this.top = scrollHeight; } }; this.timer = setInterval(updateTop, 20); } }, }, beforeUnmount() { clearInterval(this.timer); }, }; </script> <style> .scroll-notice { width: 100%; height: 40px; } .notice-content { position: relative; } .notice-item { line-height: 40px; } </style> ``` 在上面的示例中,我们使用了`el-scrollbar`组件来创建一个带有滚动条的容器。通知内容放置在一个具有绝对定位的容器内,并通过改变容器的`top`值来实现滚动效果。在`mounted`钩子中,我们调用`startScroll`方法来启动滚动。 当通知内容的高度大于容器的高度时,我们会通过定时器不断更新`top`值,从而实现滚动效果。当滚动到底部时,我们将`top`值重置为容器的高度,以实现循环滚动。 请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。你可能需要根据具体情况进行样式调整和数据绑定。 希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值