欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > 使用el-table实现自动滚动

使用el-table实现自动滚动

2024/10/24 8:32:23 来源:https://blog.csdn.net/weixin_38912662/article/details/140657947  浏览:    关键词:使用el-table实现自动滚动

文章目录

    • 概要
    • 技术实现
    • 完整代码

概要

在前端开发大屏的时候,我们会用到表格数据展示,有时候为了使用户体验更加好,会增加表格自动滚动。下边我将以示例代码,用element UI的el-table来讲一下。

技术实现

1 .增加dom监听,鼠标放上去的时候不滚动,鼠标离开的时候滚动

  	  let status = true;scrollDom.addEventListener('mouseover', () => {status = false;});scrollDom.addEventListener('mouseout', () => {status = true;});

2 通过判断dom的scrollHeight和scrollTop的关系,来实现滚动

	this.timer = setInterval(() => {if (status) {debuggerscrollDom.scrollTop += 1;if (scrollDom.scrollHeight - (scrollDom.clientHeight + scrollDom.scrollTop) < 1) {scrollDom.scrollTop = 0;}}}, 100);

3.当不需要使用,或者表格需要重新渲染的时候,我们需要清掉定时器

  clearInterval(this.timer);

完整代码

 startAutoScroll() {const scrollDom = this.$refs.tableList.bodyWrapper;let status = true;scrollDom.addEventListener('mouseover', () => {status = false;});scrollDom.addEventListener('mouseout', () => {status = true;});this.timer = setInterval(() => {if (status) {debuggerscrollDom.scrollTop += 1;if (scrollDom.scrollHeight - (scrollDom.clientHeight + scrollDom.scrollTop) < 1) {scrollDom.scrollTop = 0;}}}, 100);},

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com