Detailed JavaScript to implement waterfall streaming layout _javascript skills

Source: Internet
Author: User

This article introduces the JavaScript waterfall Flow layout of the relevant content, shared for everyone to refer to, the specific content as follows

JS principle

It says that the column layout is simply a complete abuse of the layout of the absolute. Absolute layout, just like 10 yuan/day of the moving brick. And the column layout is standing there watching him move the brick supervisor. Also are moving bricks, a salesman, a show IQ. It's!!!.
Listen to the force, we face a bleak life.
The principle of the column layout and the absolute layout is not too big yet.
There are also 3 parts, one is the page load adaptive, the second is sliding load, the third is responsive layout.
Explain separately:

1. Load Adaptive

Let's take a look at the code first:

var $ = function () {//One hacks return document.querySelectorAll.apply (document, arguments);} var waterfall = (function () {//initialization layout var arrheight = [];//Column height var columns = function () {//calculation page can put up how many columns var Bodyw = $ (' #container ') [0
      ].clientwidth, PINW = $ (". Column") [0].offsetwidth;
    return Math.floor (BODYW/PINW);
      //Set Waterfall center var gethtml = function () {var cols = $ ('. Column '),//Get an existing number of columns arrhtml = []; for (var i = 0, col col = cols[i++];) {var htmls = Col.innerHTML.match//gi);
      To obtain a columns arrhtml = Arrhtml.concat (HTMLS);
    return arrhtml; //Get the lowest height in the array var getminindex = function () {//Get the minimum height of index var minheight = Math.min.apply (null, arrheight);
      Get the minimum height for (var i in arrheight) {if (arrheight[i] = = minheight) {return i; }} var createcol = function () {var cols = columns (),//Get number of columns contain = $ ("#container") [0]; contain.innerhtml = ';
        Clears the data for (var i = 0; i < cols i++) {var span = document.createelement ("span");
        Span.classname = "column";
      Contain.appendchild (span);
      }///init column height var initheight = function () {var cols = columns (), arr = [];
      for (var i = 0; i < cols i++) {arr.push (0);
    } arrheight = arr;
      //Create a ele and add to the minimum position var createarticle = function (html) {var cols = $ ('. Column '), index = Getminindex (),
    Ele = document.createelement (' article ');
    Ele.classname = "Panel";;
    ele.innerhtml = html;
    Cols[index].appendchild (ele);
  Arrheight[index] + = ele.clientheight; }//traverse the obtained HTML and then add to the page var reloadimg = function (htmls) {for (var i = 0, HTML, index; HTML = htmls[i++];)
    {createarticle (HTML); The var onload = function () {var contain = $ ("#container") [0],//get container arrhtml = gethtml ();//Get all existing waterfalls Stream block//Create column, then load
      Createcol ();
      Initialization of Arrheight initheight ();
      To redraw the page reloadimg (arrhtml);
    return this;

 }
})();

See a program, first find his entry function, obviously, in the beginning should be onload, then, observe the onload function. It can be found that there are 4 functions in it.
Because the user's width is not certain, so our number of columns is not certain. At this point, you need to get the actual size and then do a calculation. Then you need to rearrange the original data.
So, gethtml is to get the original data (InnerHTML);
You can then add columns with more width.
The Createcol function is more width to add columns.
At this point, we need an array (arrheight) to hold the height of each column (the default is 0).
Then you can rearrange the page =>reloadimg (arrhtml), arrhtml is the original data.
OK, we're here to do the primary lifting.
The next step is to start reinforcing.

2, sliding load

This should be my direct copy come over, so say, function write good, reusability is also Bang Bang.

Show U Code

 var isload=function () {//whether can be loaded
    var scrolltop = Document.documentElement.scrollTop | | document.body.scrolltop,< C2/>wholeheight = Document.documentElement.clientHeight | | Document.body.clientHeight, point
      = scrolltop + wholeheight///page bottom distance from header
    var Lasthei = Math.min.apply ( Null,arrheight);
    Return (Lasthei < point)? True:false;
  }

  var Dealscroll = (function () {
    window.onscroll = () =>{dealscroll ();}
    var container = $ (' #container ') [0];
    return function () {
      if (Isload ()) {for
        (var i = 0,html,data;data = dataint[i++];) {
          html = tpl.temp (DATA.SRC);//Get data then add template
          createarticle (HTML);
        }
      return this;
    }
  }) ();

The same isload, the same dealscroll logic. The point to note here is that createarticle is the function that adds bricks to the lowest height column.
Then, there is no then.

3. Responsive layout

This I also directly copy come over.

 var resize = (function () {
    window.onresize = () =>{resize ();};
    var flag;
    return function () {
      cleartimeout (flag);
      Flag = settimeout (() =>{onload ();},500);
      return this;
    }

What needs to be explained is, onload,dealscroll,resize these 3 functions, I add "return this" later. The goal is to make chained calls for later reuse needs.

The above is the entire content of this article, I hope to learn JavaScript waterfall flow to help.

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.