使用监听 scroll 可以实现监听页面滚动或者元素滚动是否到底
案例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <template> <div class="app-container"> <div class="box"> <div v-for="(item,index) in count" :key="index" class="item"> {{ item }} </div> </div> </div> </template>
<script> export default { name: "test", data() { return { count: 20 } }, mounted() { this.handlerNodeScroll() }, methods: { handlerNodeScroll() { let that = this let fun = that.debounce(e => { let scrollTop = e.target.scrollTop; let clientHeight = e.target.clientHeight; let scrollHeight = e.target.scrollHeight; if (Math.ceil(scrollTop + clientHeight) >= scrollHeight) { console.log('滚动到底部'); if (that.count >= 50) return that.count += 10 } }, 500) let box = document.querySelector(".box"); box.addEventListener("scroll", function (e) { fun(e) }) }, debounce(handle, delay) { let timer = null; return function () { let _self = this, _args = arguments; clearTimeout(timer); timer = setTimeout(function () { handle.apply(_self, _args) }, delay) } } } } </script>
<style scoped lang="scss"> .box { width: 200px; height: 400px; border: 1px solid #7c7c7c; padding: 15px; box-sizing: border-box; overflow: auto;
.item { margin-bottom: 10px; height: 40px; line-height: 40px; text-align: center; background-color: #1482f0; font-size: 18px; font-weight: 700; color: white; } } </style>
|
运行效果
