With the popularity of Pinterest, waterfall streaming layout is used by more and more sites, this layout does have a lot of benefits, the Picture List page has a strong visual appeal, but also improve the user "find a good map" efficiency. Waterfall flow to achieve a number of ways, before the Taobao ued has a detailed article on the pros and cons of various ways. Today we discuss the absolute sort to realize the waterfall flow way namely Pinterest adopts the way.
First of all, the waterfall flow sorting algorithm, reference Demo1, the idea is very simple, we split the waterfall into three parts to see: Containers, columns, lattice
1. First calculate the current screen can accommodate a number of waterfalls, the value is "down the whole (screen visible area width/(lattice width + spacing)";
2. In order to ensure the center of the container, set the width of the container to the number of columns * (lattice width + spacing)-spacing, you should note that when the width of the container is calculated and then displayed, otherwise it will cause the page width jitter, affect the experience.
3. Sorting begins with the first n (n columns) in each column, respectively, then each time you look for the smallest column, put the grid in (the left value is column ordinal * (lattice width + spacing), top value is column height + spacing), and refresh the height of the column, traversing all the squares until all the squares are sorted.
Finally, the event handle is bound to window.onload and window.onresize, and a waterfall-flow layout page comes out.
Such a sorting algorithm looks very beautiful, can be really combined with asynchronous loading data applied to the page to solve the following several problems
1. When zooming the browser window, the event will be constantly triggered, if the response every time will be a loss of performance, you need to zoom in after the end of the method to rearrange.
2. Page scrolling to the bottom after the request data is successful, only the new node is queued.
3. If the server is unable to give the picture height, you need to rearrange the picture after it has finished loading.
The first question I use settimeout and cleartimeout to solve, the train of thought is after the window changes to start the time, if the window is still changing to start the timer, the window no longer changes then the delay (very short time) triggers the rearrangement event. For the time being, there should be a better way.
The code is as follows
var re; Window.onresize = function () {cleartimeout (re); re = settimeout (resize,100);
The second problem is that if you have new data loads each time, you want to rearrange the nodes in the entire container, which is very consuming performance. Solution Idea:
1. Save the column in a global array, update the array data after each rearrangement or new grid, so that you can call it directly the next time the sorting algorithm is executed.
2. The new lattice is saved in the array as a parameter passed to the sorting algorithm, only to traverse and operate the newly lattice.
The third problem is that if the server cannot give the picture size, then you must be completely loaded after the picture can be sorted (because the height is real-time acquisition, the picture is not full height error), there is no good way, can only traverse the picture, each picture loaded successfully executed after a callback function, will load the number of successful pictures + 1, when loading the number of successful pictures equals the total number of pictures to perform the sorting method. The disadvantage is that there is a picture loading can not see all of the real project or need to load data asynchronously when the picture size.
Well, the above is the waterfall in the implementation of the process encountered problems and solutions, from the beginning of the load 3-4 screen on the card to die to now can be unlimited load (ff,chrome), deeply optimize the need for JS and infinite. A little bit of experience written here right when you think about the waterfall flow to achieve the optimization of the views of the welcome to exchange discussions.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.