script:
mounted () { window.addEventListener('scroll', this.handleScroll, true); },
methods: { handleScroll () { if (!this.$refs.categoryTable) { return; } let offsetTop = this.$refs.categoryTable.$el.getBoundingClientRect().top; if (offsetTop < 160) { this.isFixed = true; } else { this.isFixed = false; } } }
template
<el-table class="table-width" :data="categoryList" ref="categoryTable" @scroll.native="handleScroll" ></el-table>
按照以上方式就可以监听到滚动事件了,getBoundingClientRect()使用这个方法获取当前元素的位置