简体   繁体   English

javascript-重新创建pinterest,例如网格

[英]javascript - recreation of pinterest like grid

I'm creating a grid like pinterest and everything is working fine except that getting the height of items in the code is too slow. 我正在创建一个像pinterest这样的网格,除了在代码中获取项目的高度太慢之外,其他所有工作都很好。
in my code, I have to get the height of last added element and add it to the height of the row, so for the next item I can set the top position to the height. 在我的代码中,我必须获取最后添加的元素的高度并将其添加到行的高度,因此对于下一个项目,我可以将顶部位置设置为该高度。
here's the code: 这是代码:

testOne: function(itemArray, parent){
            // layout
            var layout = [];
            for(i in itemArray_){
                var item = itemArray[i];
                var template = this.template(item);
                // get smallest height
                var smallestHeight = layout[0];
                var smallestHeightRow = 0;
                for(i in layout){if(layout[i] < smallestHeight){smallestHeight = layout[i]; smallestHeightRow = i;}}
                // add item to screen
                parent.append(template);
                // add height of last item
                var item = parent.find("[item-id="+item._id+"]");
                item.css("top", smallestHeight+15+"px");
                item.css("left", smallestHeightRow*25+"%");
                var itemHeight = item[0].offsetHeight;
                layout[smallestHeightRow] = smallestHeight+itemHeight;
            }
        },

but the problem is, this process is fast except for getting the height of the element. 但是问题是,除了获取元素的高度之外,此过程非常快。
so without getting the height, the function takes about 20ms 因此在不获取高度的情况下,该功能大约需要20毫秒
but getting the height adds another 75ms to the process and makes it about 95ms 但是获取高度会增加75ms的时间,使其大约95ms
now my question is, how can I make the determination of the height a lot faster? 现在我的问题是,我怎样才能更快地确定高度?

添加完第一轮图块之后,您可以存储不同行的高度。因此,下一次在您添加新图块时,它将可用,而无需计算或等待为了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM