• 技术文章 >web前端 >js教程

    监听element-ui table滚动事件的代码示例

    不言不言2019-03-26 10:29:48转载1793

    本篇文章给大家带来的内容是关于监听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的绑定!

    本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的JavaScript视频教程栏目!

    以上就是监听element-ui table滚动事件的代码示例的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除
    专题推荐:javascript
    上一篇:JavaScript操作剪贴板的实现方法介绍 下一篇:Vue源码之目录结构的简单分析
    大前端线上培训班

    相关文章推荐

    • springboot和element-axios如何实现跨域请求(代码)• JavaScript中getElementById怎么使用• 如何使用getElementsByClassName()从类名中获取多个HTML元素• JavaScript获取dom元素querySelector()替代getElementById()的方法

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网