小编给大家分享一下javascript如何监听element-ui table滚动事件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
背景:做管理平台的项目,用到了element-ui,需要通过监听el-table滚动的位置来获取最新的数据,那么怎么样监听el-table的滚动呢?
准备:我们默认的技术栈是 vue+element-ui
template代码:
<el-table
:data="logList"
:show-header="false"
row-class-name="table-row-class"
height="700"
ref="table"
@row-click="rowClick">
<el-table-column type="expand">
<el-table-column
label="log信息"
prop="message">
</el-table-column>
</el-table>
绑定监听事件
mounted() {
// 获取需要绑定的table
this.dom = this.$refs.table.bodyWrapper
this.dom.addEventListener('scroll', () => {
// 滚动距离
let scrollTop = this.dom.scrollTop
// 变量windowHeight是可视区的高度
let windowHeight = this.dom.clientHeight || this.dom.clientHeight
// 变量scrollHeight是滚动条的总高度
let scrollHeight = this.dom.scrollHeight || this.dom.scrollHeight
if (scrollTop + windowHeight === scrollHeight) {
// 获取到的不是全部数据 当滚动到底部 继续获取新的数据
if (!this.allData) this.getMoreLog()
console.log('scrollTop', scrollTop + 'windowHeight', windowHeight + 'scrollHeight', scrollHeight)
}
})
}
获取到新的数据后,调整滚动条的位置
getMoreLog() {
...
this.dom.scrollTop = this.dom.scrollTop - 100
...
}
结语:至此我们已经完成了对table的绑定!
以上是“javascript如何监听element-ui table滚动事件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。