


Analyze the principle of javascript waterfall flow to achieve scrolling loading of images_javascript skills
Let’s learn about waterfall flow first
Waterfall flow, also known as waterfall flow layout. It is a popular website page layout. The visual performance is a jagged multi-column layout. As the page scroll bar scrolls down, this layout will continuously load data blocks and append them to the current tail. The first website to adopt this layout was Pinterest, which gradually became popular in the country. Most of the fresh websites in China basically use this style, such as Meilishuo and Taobao.
This is an effect I achieved, that is, it won’t load no matter how I scroll. Just flow and flow like a waterfall!
We only talk about the Js implementation method here
Implementation principle:
Calculate for the first time the existing data block elements in the container: 1. Total width of the container 2. Column width 3. Minimum number of columns. After getting the number of columns, use an array to store all the heights of the box to find the minimum height. Then the height is updated based on the serial number; it seems a bit awkward, but it is very simple to implement.
For scrolling loading: that is, after scrolling to a height, you need to load data. In fact, this is the minimum height value of the column. In this way, comparing the current scroll value with the minimum height value can determine whether to trigger loading of data; that is Write a function to determine whether the conditions for loading images are met. If so, start loading. For example, get the offsetTop, visual area height, and scrolling distance of the last picture. That is, when the offsetTop of the picture is less than the sum of the visual area height and the scrolling distance, it should be loaded at this time, but the conditions can be determined arbitrarily. You can also wait until the picture is halfway scrolled before triggering the loading condition, as shown in the picture:
Enter the HTML CSS code first
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>waterfall</title> <script src="script.js"></script> <style> * { margin: 0; padding: 0; } body { background: yellow; } #container { } #container .pin { padding-left: 15px; padding-top: 15px; float: left; } #container .div-box { float: left; border: 1px solid #ccc; box-shadow: 0 0 5px #bbb; background: #fff; padding: 12px; border-radius: 9px; } #container .div-box img { width: 300px; } #container .div-box p { text-align: center; font-size: 20px; font-weight: bold; color: red; } </style> <script> </script> </head> <body> <div id="container"> <div class="pin"> <div class="div-box"> <img src="img/1.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/2.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/3.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/4.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/5.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/6.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/7.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/8.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/9.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/10.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/1.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/2.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/3.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/4.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/5.jpg" alt=""> <p>白超华-博客园</p> </div> </div> <div class="pin"> <div class="div-box"> <img src="img/6.jpg" alt=""> <p>白超华-博客园</p> </div> </div> </div> </body> </html>
JS code, each line has comments
window.onload = function(){ var data = { //模拟后台数据 的一个JSON格式的文件 "data":[ {"src":"1.jpg"}, {"src":"2.jpg"}, {"src":"3.jpg"}, {"src":"4.jpg"}, {"src":"5.jpg"}, ] }; window.onscroll = function(){ if(checkScroll()){ //判断是否具备滚动加载得条件 var oParent = document.getElementById('container'); for(var i=0; i<data.data.length; i++){ var div1 = document.createElement('div'); //创建div元素 div1.className = 'pin'; //设置class oParent.appendChild(div1); var div2 = document.createElement('div');//创建div元素 div2.className = 'div-box'; div1.appendChild(div2); var imgs = document.createElement('img');//创建img元素 imgs.style.width = '300px'; imgs.src = 'img/'+data.data[i].src; //设置读取路径 div2.appendChild(imgs); var p = document.createElement('p');//创建p元素 p.innerHTML = '白超华-博客园'; div2.appendChild(p); } waterfall('container','pin'); //--注意 别忘了这句,当滚动时候就执行 } } waterfall('container','pin'); } function waterfall(parent, box){ var oParent = document.getElementById(parent);//获取父级对象 var aBox = getByClass(oParent,box);//获取所有class为pin的盒子的集合 var boxWidth = aBox[0].offsetWidth;//获取一个盒子的宽 var pageWidth = document.body.clientWidth||document.documentElement.clientWidth;//获取可视区宽 var cols = Math.floor(pageWidth/boxWidth);//获得列数 var arrH = [];//用于存放盒子的高 for(var i=0; i<aBox.length; i++){ if(i<cols){//当小于第一列个数的时候 arrH.push(aBox[i].offsetHeight); } else { var minH = Math.min.apply(null,arrH);//得到数组中盒字的最小高度minH; var index = getMinIndex(arrH,minH); aBox[i].style.position = 'absolute';//设置绝对定位 aBox[i].style.top = minH+'px';//设置top,就是最小高度 aBox[i].style.left = aBox[0].offsetWidth*index+'px';//设置left,就是一个盒子的宽*index索引数 arrH[index]+=aBox[i].offsetHeight; //更新新添加盒字后的列高 } } } //通过父级获取class function getByClass(parent, classname){ var aClass = parent.getElementsByTagName('*'); var arr = []; for(var i=0; i<aClass.length; i++){ if(aClass[i].className == classname){ arr.push(aClass[i]); } } return arr; } //最小值的索引index function getMinIndex(arr,val){ for( i in arr){ if(arr[i] == val){ return i; } } } // function checkScroll(){ var oParent = document.getElementById('container'); var aBox = getByClass(oParent,'pin'); var lastBoxHeight = aBox[aBox.length-1].offsetTop;// 当滚到到这个距离时候就开始加载 var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//兼容的滚动距离 var documentHeight = document.documentElement.clientHeight; //页面高度 if(lastBoxHeight<scrollTop+documentHeight){ return true; } }
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.
