Javascript implements waterfall flow to dynamically load images
This article mainly introduces in detail the principle of dynamic loading of images using JavaScript to implement waterfall flow. It has a certain reference value. Interested friends can refer to it.
The example in this article shares the js waterfall with everyone. Stream loading effect, dynamic loading of pictures, for your reference, the specific content is as follows
Mouse scroll event, when the mouse scrolls to the bottom, the picture is dynamically loaded.
1. HTML code
##
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js实现瀑布流效果-动态加载图片</title> <link rel="stylesheet" href="css/waterfallflow.css" type="text/css" /> <script src="js/waterfallflow.js"></script> </head> <body> <p id="container"> <p class="box"> <p class="box_img"> <img src="img/1.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/2.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/3.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/4.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/5.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/6.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/7.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/3.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/1.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/2.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/1.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/2.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/3.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/4.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/5.jpg" /> </p> </p> <p class="box"> <p class="box_img"> <img src="img/6.jpg" /> </p> </p> </p> </body> </html>
2. CSS code
*{ margin: 0px; padding: 0px; } #container{ position: relative; } .box{ padding: 5px; float: left; margin: 0px auto; } .box_img{ padding: 5px; border: 1px solid #DCDCDC; box-shadow: 0 0 5px #ccc; border-radius: 5px; } .box_img img{ width: 230px; }
3. JavaScript code
window.onload=function(){ imgLocation("container","box"); var imgData={"data":[{"src":"1.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"},{"src":"6.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"}]} window.onscroll=function(){ // console.log(document.documentElement.scrollTop); if(checkFlag()){ var cparent=document.getElementById("container"); for(var i=0;i<imgData.data.length;i++){ var ccontent=document.createElement("p"); ccontent.className="box"; cparent.appendChild(ccontent); var boximg=document.createElement("p"); boximg.className="box_img"; ccontent.appendChild(boximg); var img=document.createElement("img"); img.src="img/"+imgData.data[i].src; boximg.appendChild(img); //另外一种方法在p后边追加内容,不覆盖原有内容 // var content="<p class='box'><p class='box_img'><img src='img/"+imgData.data[i].src+"'/></p></p>"; // cparent.innerHTML+=content; } imgLocation("container","box"); } } } function checkFlag(){ var cparent=document.getElementById("container"); var ccontent=getChildElement(cparent,"box");//图片的所有box数 var lastContentHeight=ccontent[ccontent.length-1].offsetTop;//最后一张图片距离顶部高度 var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//滚动条距离顶部高度 var pageHeight=document.documentElement.clientHeight||document.body.clientHeight;//屏幕高度 // console.log(lastContentHeight+","+scrollTop+","+pageHeight); if(lastContentHeight<scrollTop+pageHeight){ return true; } } function imgLocation(parent,content){ var cparent=document.getElementById(parent); var ccontent=getChildElement(cparent,content);//图片的所有box数 var imgWidth=ccontent[0].offsetWidth;//图片宽度 var num=Math.floor(document.documentElement.clientWidth/imgWidth);//一行放图片个数 cparent.style.cssText="width:"+imgWidth*num+"px;margin:0px auto";//container的宽度 var boxHeightArr=[];//每一列box高度 for(var i=0;i<ccontent.length;i++){ if(i<num){ boxHeightArr[i]=ccontent[i].offsetHeight; }else{ var minHeight=Math.min.apply(null,boxHeightArr);//最小高度 var minIndex = getMinheightLocation(boxHeightArr,minHeight);//得到最小高度下标 // console.log(minHeight+","+minIndex); ccontent[i].style.position="absolute"; ccontent[i].style.top=minHeight+"px";//距离顶部高度 ccontent[i].style.left=ccontent[minIndex].offsetLeft+"px";//距离左边长度 boxHeightArr[minIndex]+=ccontent[i].offsetHeight; // console.log(ccontent[i].offsetHeight+","+ccontent[i].height);//ccontent[i].height=undefined } } } function getMinheightLocation(boxHeightArr,minHeight){ for(var i in boxHeightArr){ if(boxHeightArr[i]==minHeight){ return i } } } function getChildElement(parent,content){ //将parent下有content的全部取出 var contentArr=[]; var allContent=parent.getElementsByTagName("*"); for(i=0;i<allContent.length;i++){ if(allContent[i].className=="box"){ contentArr.push(allContent[i]); } } return contentArr; }
jQuery and CSS3 folding card drop-down list box to achieve the effect
JavaScript to use Canvas to draw graphics
The above is the detailed content of Javascript implements waterfall flow to dynamically load images. For more information, please follow other related articles on the PHP Chinese website!

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...

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.

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...

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.

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/)...

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. �...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.
