vue scroll滾動判斷的實現(xiàn)(是否滾動到底部、滾動方向、滾動節(jié)流、獲取滾動區(qū)域dom元素)
1、是否滾動到底部
isScrollBottom() { // 是否滾動到了底部 this.box = this.$refs.chatListWrapper var clientHeight = this.box.clientHeight var scrollTop = this.box.scrollTop var scrollHeight = this.box.scrollHeight if (scrollTop + clientHeight == scrollHeight) { this.$store.dispatch('setBottomBtn', false, { root: true }) // 隱藏直達最新消息按鈕 this.isBottom = true this.isTop = false } else { this.$store.dispatch('setBottomBtn', true, { root: true }) // 顯示直達最新消息按鈕 this.isTop = false this.isBottom = false if (scrollTop == 0) { this.isTop = true } } },
2、scroll滾動方向判斷
getDirection() { // scroll滾動方向~~~~ this.box = this.$refs.chatListWrapper var scrollTop = this.box.scrollTop var scroll = scrollTop - this.initTop this.initTop = scrollTop let dir = 'down' if (scroll < 0) { dir = 'up' } else { dir = 'down' } return dir },
3、滾動節(jié)流
1)、在滾動的dom上綁定scroll事件,監(jiān)聽滾動
2)、data中定義:fnScroll: () => {},
初始值
3)、mounted中給fnScroll函數(shù)賦值,_.throttle實現(xiàn)滾動節(jié)流
this.fnScroll = _.throttle(() => { }, 500)
4、獲取滾動可視區(qū)域內(nèi)dom:
實現(xiàn)注意:判斷當前元素是否在可視區(qū)域內(nèi),若在則存到isSeeDomArr中,然后循環(huán)isSeeDomArr數(shù)組,拿到當前可視區(qū)域內(nèi)的最后一個dom,再去判斷是否更新對應(yīng)的咨詢軌跡。
不要滾動時就去更新,這樣會造成不停請求更新,最后一次請求可能無效,造成數(shù)據(jù)的錯亂
sendRead() { const chatLi = document .getElementById('chat_list_wrapper') .getElementsByTagName('li') var container = this.$refs.chatListWrapper var swHeight = container.clientHeight const scrollTop = container.scrollTop const aa = swHeight + scrollTop let isSeeDomArr = [] for (let j = 0; j < chatLi.length; j++) { if (scrollTop < chatLi[j].offsetTop && chatLi[j].offsetTop < aa) { isSeeDomArr.push(chatLi[j]) //將可視區(qū)域內(nèi)所有dom存儲到isSeeDomArr } } if (isSeeDomArr.length) { // 非 ceo接診臺更新消息的已讀狀態(tài) if (this.$route.path.indexOf('diagnose/ceo') === -1) { for (let m = 0; m < isSeeDomArr.length; m++) { const isSelfSend = isSeeDomArr[m].getAttribute('isSelfSend') const msgStatus = isSeeDomArr[m].getAttribute('msgStatus') const msgType = isSeeDomArr[m].getAttribute('msgType') if (!isSelfSend && !msgStatus && msgType !== 'notice') { const _id = isSeeDomArr[m].getAttribute('id') this.sendReadApi(_id) } } } // 更新聊天對應(yīng)的咨詢軌跡 this.setCurrentFdAsk( isSeeDomArr[isSeeDomArr.length - 1].getAttribute('fdAsk') ) } },
the end:滾動加載這些判斷前前后后改了好多次,這次終于感覺邏輯比較清晰了,也算對自己有個交代。。。
到此這篇關(guān)于vue scroll滾動判斷的實現(xiàn)(是否滾動到底部、滾動方向、滾動節(jié)流、獲取滾動區(qū)域dom元素)的文章就介紹到這了,更多相關(guān)vue scroll滾動判斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue通過axios發(fā)送ajax請求基礎(chǔ)演示
這篇文章主要介紹了Vue通過axios發(fā)送ajax請求基礎(chǔ)演示,包括了axios發(fā)送簡單get請求,axios get傳參,axios發(fā)送post請求等基礎(chǔ)代碼演示需要的朋友可以參考下2023-02-02VUE table表格動態(tài)添加一列數(shù)據(jù),新增的這些數(shù)據(jù)不可以編輯(v-model綁定的數(shù)據(jù)不能實時更新)
這篇文章主要介紹了VUE table表格動態(tài)添加一列數(shù)據(jù),新增的這些數(shù)據(jù)不可以編輯(v-model綁定的數(shù)據(jù)不能實時更新),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2020-04-04vue開發(fā)中后臺系統(tǒng)復(fù)雜表單優(yōu)化技巧
這篇文章主要為大家介紹了vue開發(fā)中后臺系統(tǒng)復(fù)雜表單的優(yōu)化技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07vue?實現(xiàn)動態(tài)設(shè)置元素的高度
這篇文章主要介紹了在vue中實現(xiàn)動態(tài)設(shè)置元素的高度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08詳解實現(xiàn)vue的數(shù)據(jù)響應(yīng)式原理
這篇文章主要介紹了詳解實現(xiàn)vue的數(shù)據(jù)響應(yīng)式原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Nuxt.js實現(xiàn)一個SSR的前端博客的示例代碼
這篇文章主要介紹了Nuxt.js實現(xiàn)一個SSR的前端博客的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09