The examples in this article share with you the js waterfall flow layout learning materials for your reference. The specific content is as follows
Features: equal width and unequal height.
Implementation method: Javascript/Jquery/CSS3 multi-column layout.
Sample website: Huaban.com -->Category
1. JS to implement waterfall flow
index.html: Page structure
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>瀑布流布局</title> <link rel="stylesheet" href="styles/layout.css"> </head> <body> <div id="main"> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/0.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/1.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/2.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/3.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/4.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/5.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/6.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/7.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/8.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/9.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/10.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/11.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/12.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/13.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/14.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/15.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/16.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/17.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/18.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/19.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/20.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/21.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/22.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/23.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/24.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/25.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/26.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/27.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/28.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/29.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/30.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/31.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/32.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/33.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/34.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/35.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/36.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/37.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/38.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/39.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/40.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/41.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/42.jpg" class="lazy" alt=""></div></div> <div><div><img src="/static/imghwm/default1.png" data-src="../waterFall/pic/43.jpg" class="lazy" alt=""></div></div> </div> <script src="scripts/waterfall.js"></script> </body> </html>
layout. css: page element style
*{ pdding:0; margin:0; } div#main{ position: relative; } div.box{ padding:15px 0 0 15px; float: left; } div.pic{ padding:10px; border:1px solid #ccc; border-radius:5px; box-shadow: 0 0 5px #ccc; } .pic img{ height:auto; width:165px; }
waterfall.js
window.onload=function(){ waterFall('main','box'); //模拟后台相应数据json var dataInt={ "data": [ {"src":"0.jpg"}, {"src":"1.jpg"}, {"src":"2.jpg"}, {"src":"3.jpg"}, {"src":"4.jpg"}, {"src":"5.jpg"}, {"src":"6.jpg"} ] } window.onscroll=function(){ if(checkScrollSlide){ //将数据块渲染到当前页面的尾部 var oParent=document.getElementById("main"); for(var i=0;i<dataInt.data.length;i++){ var oBox=document.createElement("div"); oBox.className="box"; oParent.appendChild(oBox); var oPic=document.createElement("div"); oPic.className="pic"; oBox.appendChild(oPic); var img=document.createElement("img"); img.setAttribute("src",dataInt.data[i]); img.src="pic/"+dataInt.data[i].src; oPic.appendChild(img); } dataInt=null;//清空数据块,防止无限加载 waterFall('main','box');//对页面新元素进行布局渲染 } } } function waterFall(parent,box){ //将main下的class为box的所有元素取出来 var oParent=document.getElementById(parent); var oBoxs=getByClass(oParent,box); console.log(oBoxs.length); //计算整个页面显示的列数(页面宽/box宽) var oBoxW=oBoxs[0].offsetWidth; // console.log(oBoxW); var cols=Math.floor(document.documentElement.clientWidth/oBoxW); // console.log(cols); //设置main的宽 oParent.style.cssText="width:"+oBoxW*cols+"px;margin:0 auto;" var hArr=[];//存放每列高度的数组 for(var i=0;i<oBoxs.length;i++){ if(i<cols){ hArr.push(oBoxs[i].offsetHeight); }else{ var minH=Math.min.apply(null,hArr);//获取当前数组最小高度值 // console.log(minH); var index=getMinhIndex(hArr,minH);//获取数组最小高度的索引 //console.log(index); oBoxs[i].style.position="absolute";//将之后的图片依次绝对定位 oBoxs[i].style.top=minH+"px"; oBoxs[i].style.left=index*oBoxW+"px";//计算新图片所在的位置并赋值 hArr[index]+=oBoxs[i].offsetHeight;//变化数组列的高度值,因为加上了一张图片 } //console.log(hArr); } } //根据class获取元素 function getByClass(parent,clsName){ var boxArr=new Array(),//用来存储获取到的所有class为box的元素 oElements=parent.getElementsByTagName("*"); for(var i=0;i<oElements.length;i++){ if(oElements[i].className==clsName){ boxArr.push(oElements[i]); } } return boxArr; } function getMinhIndex(arr,val){ for(var i=0;i<arr.length;i++){ if(arr[i]==val){ return i; } } } //检测是否具备滚动条加载数据块条件 function checkScrollSlide(){ var oParent=document.getElementById("main"); var oBoxs=getByClass(oParent,"box"); var lastBoxH=oBoxs[oBoxs.length-1].offsetTop+Math.floor(oBoxs[oBoxs.length-1].offsetHeight/2); var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;//混杂模式和标准模式下的scrollTop获取 //console.log(scrollTop); var height=document.body.clientHeight||document.documentElement.clientHeight;//混杂模式和标准模式下的浏览器窗口高度获取 return (lastBoxH<scrollTop+height)?true:false;//检测最后一个box高度是否小于滚动高度+窗口高度,返回布尔值 }
2. JQuery
$( window ).on( "load", function(){ waterfall('main','pin'); var dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]}; window.onscroll=function(){ if(checkscrollside()){ $.each( dataInt.data, function( index, value ){ var $oPin = $('<div>').addClass('pin').appendTo( $( "#main" ) ); var $oBox = $('<div>').addClass('box').appendTo( $oPin ); $('<img alt="Learn more about js waterfall flow layout" >').attr('src','./images/' + $( value).attr( 'src') ).appendTo($oBox); }); waterfall(); }; } }); /* parend 父级id pin 元素id */ function waterfall(parent,pin){ var $aPin = $( "#main>div" ); var iPinW = $aPin.eq( 0 ).width();// 一个块框pin的宽 var num = Math.floor( $( window ).width() / iPinW );//每行中能容纳的pin个数【窗口宽度除以一个块框宽度】 //oParent.style.cssText='width:'+iPinW*num+'px;ma rgin:0 auto;';//设置父级居中样式:定宽+自动水平外边距 $( "#main" ).css({ 'width:' : iPinW * num, 'margin': '0 auto' }); var pinHArr=[];//用于存储 每列中的所有块框相加的高度。 $aPin.each( function( index, value ){ var pinH = $aPin.eq( index ).height(); if( index < num ){ pinHArr[ index ] = pinH; //第一行中的num个块框pin 先添加进数组pinHArr }else{ var minH = Math.min.apply( null, pinHArr );//数组pinHArr中的最小值minH var minHIndex = $.inArray( minH, pinHArr ); $( value ).css({ 'position': 'absolute', 'top': minH + 15, 'left': $aPin.eq( minHIndex ).position().left }); //数组 最小高元素的高 + 添加上的aPin[i]块框高 pinHArr[ minHIndex ] += $aPin.eq( index ).height() + 15;//更新添加了块框后的列高 } }); } function checkscrollside(){ var $aPin = $( "#main>div" ); var lastPinH = $aPin.last().get(0).offsetTop + Math.floor($aPin.last().height()/2);//创建【触发添加块框函数waterfall()】的高度:最后一个块框的距离网页顶部+自身高的一半(实现未滚到底就开始加载) var scrollTop = $( window ).scrollTop()//注意解决兼容性 var documentH = $( document ).height();//页面高度 return (lastPinH < scrollTop + documentH ) ? true : false;//到达指定高度后 返回true,触发waterfall()函数 }
3. CSS multi-column layout
.container{ -webkit-column-width:160px; -moz-column-width:160px; -webkit-column-gap:5px; -moz-column-gap:5px; } /*数据块 砖块*/ .container div{width:160px; margin:4px 0;}
[css3 and js implementation Comparison of methods】
--css3 method--
1: No calculation required, the browser automatically calculates, just set 1 column width, high performance
2: Column width changes with the browser width size Change, the user experience is not good;
3: The pictures are sorted in vertical order, disrupting the order of picture display
4. Picture loading still requires js
--js method--
js implemented waterfall Streaming does not have the above shortcomings, but the performance is relatively poor!
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to in-depth study of js waterfall flow layout, please pay attention to the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools