vue 实现无缝向左滚动 鼠标悬停、离开时停止、开始滚动

用的引用vue文件方式写的,可以改成组件代码

html代码:

<div class="cont">
	<div class="contlist" ref='contlist' @mouseover="stopScroll" @mouseout="startScroll">
		<ul>
			<li v-for="(item, index) in list" :key="index">
				{{ index + 1 }} 、<em>{{ item.name }}</em> {{ item.time }}
			</li>
		</ul>
		<ul>
			<li v-for="(item, index) in list" :key="index">
				{{ index + 1 }} 、<em>{{ item.name }}</em> {{ item.time }}
			</li>
		</ul>
	</div>
</div>

js 代码:

<script type="text/javascript">
	var vm = new Vue({
		el: '#app',
		data: {
			list: [
				{name: '负限位报警', time: '2019-12-18 16:20:34'},
				{name: '水位报警', time: '2019-12-18 16:20:34'},
				{name: '正限位报警', time: '2019-12-18 16:20:34'},
				{name: '温度报警', time: '2019-12-18 16:20:34'},
				{name: '回架报警', time: '2019-12-18 16:20:34'},
			],
			scrollW: 0, //记录滚动到哪了
		},
		methods: {
			//鼠标悬停,停止滚动
			stopScroll () {
				var target = this.$refs.contlist;
				clearInterval(this.scrollTime)
			},
			//鼠标移开 ,接着滚动
			startScroll () {
				var target = this.$refs.contlist;
				this.scrollW = target.offsetLeft; // 移开时记录向左移动的距离
				this.scroll()
			},
			//循环滚动
			scroll (){
				var that = this; //因为定时器里要使用vue对象时不能用this, 在定时器中的 this 代表
				var target = this.$refs.contlist; 
				var initLeft = 0;
				if(this.scrollW < 0){
					initLeft = this.scrollW * (-1)
				}
				//定时器
				this.scrollTime = setInterval(function(){
				    initLeft ++;
				    if(initLeft >= target.offsetWidth / 2 ){
				        initLeft = 0;
				    }
					target.style.left = '-'+ initLeft +'px';//获取不到translateX的值改用 left
					
					//target.style = 'transform: translateX(-'+ initLeft +'px)';
				},16)
			}
		},
		mounted() {
			//实例挂载完毕前
			//调用滚动代码
			this.scroll()
		}
	})
</script>

css 代码:

<style type="text/css">
	ul,li,em{
		margin:0;
		padding: 0;
	}
	.cont{
		height: 20px;
		background-color:#000;
		color: #fff;
		overflow: hidden;
		position: relative;
	}
	.contlist{
		position: absolute;
		white-space: nowrap;
		display: flex;
		flex-direction: row;
	}
	.contlist ul{
		display: flex;
		flex-direction: row;
	}
	.contlist ul li{
		font-size: 12px;
		margin-right: 25px;
		height: 20px;
		line-height: 20px;
		display: flex;
		flex-direction: row;
	}
	.contlist ul li em{
		color:#f80;
		font-size: 12px;
		padding-right: 10px;
	}
</style>

本文转载于:https://blog.csdn.net/Amandaandjack861007/article/details/103618548

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据你的要求,我给你提供一个简单的代码示例: ```html <template> <div class="notice-wrapper"> <div class="notice-content" v-for="(notice, index) in notices" :key="index" ref="notice"> <span>{{ notice }}</span> </div> </div> </template> <script> export default { data() { return { notices: ['公告1', '公告2', '公告3'], // 公告列表 intervalId: null, // 定器ID }; }, mounted() { this.startScroll(); // 组件挂载开始滚动 }, methods: { // 开始滚动 startScroll() { this.intervalId = setInterval(() => { const firstNotice = this.$refs.notice[0]; const height = firstNotice.offsetHeight; this.$refs.notice[0].style.marginTop = `-${height}px`; this.notices.push(this.notices.shift()); }, 2000); }, // 停止滚动 stopScroll() { clearInterval(this.intervalId); }, }, // 监听鼠标进入和离开事件 created() { this.$parent.$on('mouse-enter', this.stopScroll); this.$parent.$on('mouse-leave', this.startScroll); }, // 解绑事件 beforeDestroy() { this.$parent.$off('mouse-enter', this.stopScroll); this.$parent.$off('mouse-leave', this.startScroll); }, }; </script> <style> .notice-wrapper { height: 50px; overflow: hidden; } .notice-content { line-height: 50px; height: 50px; transition: margin-top 0.5s linear; } </style> ``` 在这个示例中,我们使用v-for指令遍历公告列表,使用CSS实现滚动效果。在组件挂载,调用startScroll方法开始滚动,使用setInterval实现滚动。在鼠标进入和离开事件中,分别调用stopScroll和startScroll方法,实现鼠标移出停止滚动的效果。注意,在监听事件和解绑事件,我们使用了$parent来访问父组件,因为鼠标事件是在父组件中触发的。 希望这个示例可以帮到你,如果还有什么不明白的地方,可以继续问我哦~

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值