- 在一个vue项目中,我需要实现是一个竖向两栏的瀑布流,我采取的是在布局中放了左右两个盒子,将从接口中获取的数据,根据两个盒子的高度将数据一个一个放入当中,谁的ClientHeight小,就放进谁里面,但是在窗口滚动的过程中,获取到左右盒子的ClientHeight比实际的小,导致后面,数据进入时位置出现错误
-
`
let vm = new Vue({ el: "#waterfall", data: { "recommendList": [], "pageRecommends": [], "recommendTotal": 0, "curPage": 1, "ltList": [], "rtList": [], "leftHeight": 0, "rightHeight": 0 }, methods: { getRecommoned: function (page,size = 10) { $.get('...', { 'id': this.weng.id, 'page': page, 'size': size }, (res) => { this.recommendList = [...this.recommendList, ...res.body.list]; this.pageRecommends = res.body.list; this.recommendTotal = res.body.total; vm.$nextTick(() => { vm.updateWaterfall(); }); }) }, updateWaterfall () { this.leftHeight = this.$refs.recslt.clientHeight; this.rightHeight = this.$refs.recsrt.clientHeight; let item = vm.pageRecommends.shift(); if (item == null) { return; } if (this.leftHeight <= this.rightHeight) { vm.ltList.push(item); } else { vm.rtList.push(item); } this.$nextTick(() => { vm.updateWaterfall(); }) } }, created() { this.getRecommoned(this.curPage); window.onscroll = () => { if (this.ltList.length + this.rtList.length >= this.recommendTotal) { return; } let scrollTop = getScrollTop(); let windowHeight = getClient().height; let scrollHeight = getScrollHeight(); if (scrollTop + windowHeight == scrollHeight) { this.curPage ++; this.getRecommoned(this.curPage); } }; } }); function getClient() { return { width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight } } function getScrollTop() { return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; } function getScrollHeight() { return document.documentElement.scrollHeight || document.body.scrollHeight; }
`