1.如下图,需要绑定两个id,第一个id是需要浮动的元素,加上scroll方法监听滑块变化,第二个id是其子元素。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/34057c46cc9203895d53d45d299b28b9.png)
2.给eagleMapContainer设置overflow属性和滑块样式,CSS参考如下
#eagleMapContainer{
overflow-y: auto;
margin-top: 10px;
min-height: 150px;
max-height: 600px;
}
#eagleMapContainer::-webkit-scrollbar {
width: 6px;
height: 6px;
}
#eagleMapContainer::-webkit-scrollbar-track{
background-color:rgba(0,0,0,0.1);
}
#eagleMapContainer::-webkit-scrollbar-thumb{
border-radius: 6px;
background-color: rgba(0,0,0,0.2);
}
#eagleMapContainer::-webkit-scrollbar-corner {
background:rgba(0,0,0,0.2);
}
3.在methods添加如下方法监听滑动
hanldeScroll(e) {
const boxHeight = document.getElementById('eagleMapContainer').offsetHeight
const tableHeight = document.getElementById('table_list').offsetHeight
if (tableHeight - (e.target.scrollTop + boxHeight) < 50 && !this.loading && this.listPage < (this.tableList.length / 300)) {
if (!this.scrollTop) {
this.scrollTop = e.target.scrollTop
}
this.queryMoreStat(true, tableHeight, boxHeight)
} else if (e.target.scrollTop === 0 && !this.loading) {
this.queryMoreStat(false, tableHeight, boxHeight)
}
}
4.在methods添加如下方法,滑块置顶或触底(实现原理:始终只渲染当前300条和前后的300条,一共900条数据)
queryMoreStat(type, tableHeight, boxHeight) {
this.loading = true
if (type) {
this.listPage = this.listPage + 1
const centerPage = this.listPage * 300
const startPage = centerPage >= 300 ? centerPage - 300 : centerPage
const endPage = centerPage + 600
const newList = this.tableList.slice(startPage, endPage)
if (this.listPage > 0) {
const box = document.getElementById('eagleMapContainer')
box.scrollTop = this.scrollTop + 50
}
this.list = newList
} else {
if (this.listPage > 0) {
this.listPage = this.listPage - 1
const centerPage = this.listPage * 300
const startPage = centerPage >= 300 ? centerPage - 300 : centerPage
const endPage = centerPage + 600
const newList = this.tableList.slice(startPage, endPage)
if (this.listPage > 0) {
const box = document.getElementById('eagleMapContainer')
box.scrollTop = tableHeight - this.scrollTop - boxHeight
}
this.list = newList
} else {
this.list = this.tableList.slice(0, 300)
}
}
this.$nextTick(() => {
this.loading = false
})
}
如果帮助到你,希望你给个赞,如果没有博客账号,那就注册个,这是你大神路上的必经之路