vue 使用js写的瀑布流展示图片,滚动的时候导致第一张图片重叠,先展示,后计算图片显示?

<template>
    <div id="box">
         <div class="item" v-for='list in ceoPhoto'>
             <img :src="list.picUrl" alt="">
         </div>
    </div>
</template>
<script>
import axios from 'axios'
export default {
      name: 'Ceo',
      data () {
        return {
            pullRefreshss:true,
            ceoPhoto:[
                  {
                      'picUrl':'../../../static/image/demo/1.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/2.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/3.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/4.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/5.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/6.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/7.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/8.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/9.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/10.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/11.jpg'
                  }
            ],
            ceoPhotoS:[
                  {
                      'picUrl':'../../../static/image/demo/12.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/13.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/14.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/15.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/16.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/17.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/18.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/19.jpg'
                  },{
                      'picUrl':'../../../static/image/demo/20.jpg'
                  }
            ]
        }
      },
      created () {

      },
      watch: {},
      mounted(){
          this.init();
    },
    methods:{
        init(){
            this.waterFall();
            this.pullRefresh();
        },
        waterFall(){
            var box = document.getElementById('box');
            var items = box.children;
            // 定义每一列之间的间隙 为10像素
            var gap = 10;
             // 1- 确定列数  = 页面的宽度 / 图片的宽度
            var pageWidth = this.getClient().width;
            var itemWidth = items[0].offsetWidth;
            var columns = parseInt(pageWidth / (itemWidth + gap));
            var arr = [];
            for (var i = 0; i < items.length; i++) {
                if (i < columns) {
                    // 2- 确定第一行
                    items[i].style.top = 0;
                    items[i].style.left = (itemWidth + gap) * i + 'px';
                    arr.push(items[i].offsetHeight);
                } else {
                    // 其他行
                    // 3- 找到数组中最小高度  和 它的索引
                    var minHeight = arr[0];
                    var index = 0;
                    for (var j = 0; j < arr.length; j++) {
                        if (minHeight > arr[j]) {
                            minHeight = arr[j];
                            index = j;
                        }
                    }
                    // 4- 设置下一行的第一个盒子位置
                    // top值就是最小列的高度 + gap
                    items[i].style.top = arr[index] + gap + 'px';
                    // left值就是最小列距离左边的距离
                    items[i].style.left = items[index].offsetLeft + 'px';
                    // 5- 修改最小列的高度 
                    // 最小列的高度 = 当前自己的高度 + 拼接过来的高度 + 间隙的高度
                    arr[index] = arr[index] + items[i].offsetHeight + gap;
                }
            }
            this.pullRefreshss = true;
        },
        getClient(){
            return {
                width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
            }
        },
        //获取文档总高度
        getScrollTop(){
            var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
            if (document.body) {
                bodyScrollTop = document.body.scrollTop;
            }
            if (document.documentElement) {
                documentScrollTop = document.documentElement.scrollTop;
            }
            scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
            return scrollTop;
        },
        //浏览器视口高度
        getScrollHeight(){
            var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
            if (document.body) {
                bodyScrollHeight = document.body.scrollHeight;
            }
            if (document.documentElement) {
                documentScrollHeight = document.documentElement.scrollHeight;
            }
            scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
            return scrollHeight;
        },
        //浏览器视口的高度
        getWindowHeight(){
             var windowHeight = 0;
            if (document.compatMode == "CSS1Compat") {
                windowHeight = document.documentElement.clientHeight;
            } else {
                windowHeight = document.body.clientHeight;
            }
            return windowHeight;
        },
        pullRefresh(){
            var _this = this;
            $(window).scroll(function () {
                _this.scrollChange();
                let scrollTop = $(this).scrollTop();
                if(scrollTop>=50){
                    _this.scrollTop = true;
                }else{
                    _this.scrollTop = false;
                }
            })
        },
        scrollChange(){
            this.scollY = this.getScrollTop() + this.getWindowHeight() - this.getScrollHeight();
            // 把下拉刷新置为false,防止多次请求
            if(this.scollY >= -50){
                if(!this.pullRefreshss){
                    return false;
                }
                this.initpush();
                
            }else{
                this.pullRefreshss = true;
            }
        },
        initpush(){
            this.pullRefreshss = false;
            for(var i = 0 ; i<this.ceoPhotoS.length;i++){
                this.ceoPhoto.push(this.ceoPhotoS[i]);
            }
            this.waterFall();
        }
    }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
* {
    margin: 0;
    padding: 0;
    position: relative;
}
img {
    width: 220px;
    display: block;
}
.item {
    box-shadow: 2px 2px 2px #999;
    position: absolute;
}
</style>

显示出来的效果:

clipboard.png
再次请求时盖住了第一个图片,因为绑定问题求解决:

clipboard.png

阅读 2.7k
1 个回答

测试下了你的DEMO,你的TOP计算有点问题
你吧所有图片 换一张等比例的图片,你就知道问题了

推荐问题