一個簡單的獲取內容的辦法
表格部分,主要是ref寫一下
<el-table :data="tableData" ref="tableRef"> </el-table>
進入頁面的時候綁定監聽
mounted(){
// 綁定滾動事件this.$nextTick(() => {const table = this.$refs.tableRef;const scrollBody = table.$el.querySelector(".el-table__body-wrapper");// 1. 手動綁定滾動事件scrollBody.addEventListener("scroll", this.fangdou);// 2. 初始計算一次可視行this.fangdou();// 3. 監聽窗口變化(可選)window.addEventListener("resize", this.fangdou);});
}
函數部分,增加了一層防抖。滾動停止再執行,不然執行的太頻繁了
邏輯就是算出可視區域的高度,然后行要固定高,每行的高度你算一下或者寫個固定的多少,這樣就可以通過可視高度/行高得到,目前一次能拿到多少條。然后通過滾動條到頂部的位置距離,算出已經滾動過去多少個了,最后算出來的數量在表格內數組里抽數據
methods(){fangdou(e) {// 清除之前的定時器if (this.scrollTimer) clearTimeout(this.scrollTimer);// 設置新的定時器(延遲500ms執行)this.scrollTimer = setTimeout(() => {this.handleScroll();}, 500);},handleScroll() {const table = this.$refs.tableRef;const scrollBody = table.$el.querySelector(".el-table__body-wrapper");// 獲取滾動位置和可視區域高度const scrollTop = scrollBody.scrollTop;const visibleHeight = scrollBody.clientHeight;// 計算起始行和結束行索引const startIndex = Math.floor(scrollTop / 50);const endIndex = Math.min(startIndex + Math.ceil(visibleHeight / 50), this.tableData.length - 1);// 獲取可視行數據const visibleRows = this.tableData.slice(startIndex, endIndex);this.wdArr = [];visibleRows.forEach((item) => {// 未讀狀態,并有考核的數據才查看是否已讀if (item.is_read !== 1 && item.khf) {this.wdArr.push(item.id);}});// 這里寫你要做的事if (this.wdArr.length > 0) {this.edit_read();}},
}