首先我們用ref綁定一個 scrollbar
<el-scrollbar style="height: 100%;" ref="chatScrollRef" @scroll="scrollTest">
用scroll觸發滾動事件,一路滾到最底下,觀察三個屬性
const scrollTest = ({scrollTop}) => {console.log(scrollTop);const wrap = chatScrollRef.value?.wrapRefif (wrap) {console.log("------" + wrap.scrollHeight);console.log("++++++" + wrap.clientHeight);}
}
得出結論,當 scrollTop + clientHeight = scrollHeight
的時候,滾動條會達到最低端
1. 得到滾動條距離頂端高度
先綁定ref
const wrap = chatScrollRef.value?.wrapRef
console.log(wrap.scrollTop);
2. 將滾動條調整在最低端
先綁定ref
const scrollToBottom = () => {const wrap = chatScrollRef.value?.wrapRefif (wrap) {wrap.scrollTop = wrap.scrollHeight - wrap.clientHeight}})