JavaScript for waterfall flow layout and automatic page loading

Source: Internet
Author: User

Waterfall layouts are suitable for small chunks of data, each with similar content and no emphasis. In general, as the page scroll bar scrolls down, this layout also continuously loads the data block and attaches it to the current trailer. So, our image is called this type of layout as waterfall flow layout.

Prior to this, we implemented this type of layout with CSS, usually in the following two ways:

1. Traditional multi-column floating

    • The columns are fixed width and left floating;
    • The block of data in one column is a group, and each data block in the column is arranged sequentially;
    • When more data is loaded, it is necessary to insert different columns separately;

Although this layout is simple, there is no need to know the height of the data block explicitly. However, if the size of the browser window changes, only the X column can be fixed, it is difficult to adjust the data block arrangement.

2.CSS Multi-Column layout

You need to use one of the properties in CSS3: column. We need to specify the number of columns, column spacing, column middle border, column width of the container to implement

  #container {        -webkit-column-count:5;        /*-webkit-column-gap:10px;        -webkit-column-rule:5px solid #333;        -webkit-column-width:210px;*/        -moz-column-count:5;        /*-moz-column-gap:20px;        -moz-column-rule: 5px solid #333;        -moz-column-width:210px;*/        column-count:5;        /*column-gap:10px;        column-rule:5px Solid #333;        column-width:210px;*/    }
However, we consider such a problem, the implementation of such methods is from top to bottom to a certain height, and then add the remaining elements to the next column in turn, this is completely contrary to what we thought before. Therefore, it is more suitable for text multi-column arrangement.

So, let's consider using JavaScript to achieve the waterfall flow layout under absolute positioning.

Let's talk about the realization of the idea:

As for how we create a project, how to add the picture resources into these phala trivial things, I will no longer say (Ps. hahaha ~ ~ ~ How to do, is lazy. Also Wayward O (∩_∩) o)

In the HTML file, we first use a few div models to act as containers, containing the content we want to put in, and here we use the image Waterfall stream form to show

<body><div id= "Contenter" > <div class= "box" > <div class= "box_img" >  </div> </div> <div class= "box" > <div class= "box_img" >  </div> </div> <div class= "box" > <div C lass= "Box_img" >  </div> </div> <div class= "box"    > <div class= "box_img" >  </div> </div> <div class= "box" > <div class= "box_img" >  </div&    Gt        </div> <div class= "box" > <div class= "box_img" >  </div> </div> <div class= "box" > <div class= "box_img" >  </div> <div class= "box" > <div class= "box_img" >  </div> </div> <div class=" box "> <div class=" box_img ">  </div> </div> <div class= "box" > <div class= "b Ox_img ">  </div> </div></div></body>
At the beginning, we load 10 images by default, and then we change their style in the CSS file.

*{    margin:0;    padding:0;} /* The parent container uses a relative positioning method */#contenter {    position:relative;} /* Float */.box{float:left to the left    ;    padding:5px;}. box_img{    padding:5px;    border:1px solid #cccccc;    box-shadow:0 0 5px #cccccc;    border-radius:5px;}. Box_img img{    width:200px;    Height:auto;}
Finally, in the JS file, we begin to implement the waterfall flow layout concretely.

    • We first find the parent element, just as we go to pick fruit, we must first find the tree and then go to climb trees to pick fruit. After we find the parent element, we take the content out of the parent element and store it in a variable.
    • Before we set the style, we set the width of each picture to be the same. So how do we determine the number of columns to be placed in each row? It should be the whole screen [width/Picture width] Rounding!
    • We traverse the entire array of images to get the height of each image in an array to get the minimum height
    • Position the remaining picture below the picture with the minimum height
    • Then listen to the scroll bar, check whether loading to the last, how to judge?
    • We get the height of the last picture, and when it's height < scrollbar distance above the height + page height, we can be sure that this page has reached the end, we can load another page.
/** * Created by PC on 2015/8/28.    */window.onload = function () {imglocation ("Contenter", "box"); var imgdata = {"Data": [{"src": "img12.jpeg"},{"src": "img13.jpg"},{"src": "img14.jpg"},{"src": "img15.jpeg"},{"src": "   Img16.jpeg "},{" src ":" img17.jpeg "},{" src ":" Img18.jpeg "}]}; /* Constantly loading new content */Window.onscroll = function () {if (Checkflag ()) {var parentid = document.getElementById ("C            Ontenter ");                for (var i =0;i
Finally, is the final realization of the effect (my family dear love and Rin, do not stop me, crazy lick screen ~ ~ ~)





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript for waterfall flow layout and automatic page loading

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.