container.scrollTop 一直爲0
不能賦值javascript
watch: { historyList () { this.$nextTick(() => { const container = this.$el.querySelector('.scrolldivmain') console.log(container.scrollHeight) console.log(container.scrollTop) this.$refs.scrolldiv.scrollTo(0, container.scrollHeight + 'px') container.scrollTop = container.scrollHeight container.scrollTop(0, container.scrollHeight) console.log(container.scrollTop) // container.scrollTop 一直爲0 }) } }
window.addEventListener('scroll', () => { var scrollTop = this.$el.querySelector('.scrolldivmain') // console.log(scrollTop.scrollHeight) console.log(scrollTop.scrollTop) // 查看打印的值是否有變化 若是有 則說明滾滾動條在這個標籤中 // scrollTop.scrollTop = scrollTop.scrollHeight // 能夠嘗試下 滾動滾動條。一直在底部則能夠設置成功 }, true)
須要用到的地方 調用this.scrollToBottom()便可html
<!--element-ui--> <el-main class="scrolldivmain"> some code </el-main>
methods: { // 滾動到底部 scrollToBottom () { this.$nextTick(() => { setTimeout(() => { var scrollTop = this.$el.querySelector('.scrolldivmain') scrollTop.scrollTop = scrollTop.scrollHeight }, 13) }) } }