


Detailed explanation of the steps to implement waterfall flow layout with ajax (with code)
This time I will bring you ajax implementationWaterfall flow layoutDetailed explanation of the steps (with code), what are the notes for ajax to implement waterfall flow layout, the following is a practical case, one Get up and take a look.
Waterfall flow is currently a popular website interface layout method. The uneven multi-column layout and the way of reaching the bottom automatic loading make the website visually and user-friendly. can be greatly improved. The first person to use this layout was the foreign picture website Pinterest. Later, some domestic picture websites also began to use the waterfall flow layout, including Huaban.com, the picture community Lofter, Meilishuo, Mogujie, etc., which are similar to Pinterest.
The layout of waterfall flow should be very simple for most people, with only a few columns. The most important thing about waterfall flow is asynchronous loading of data.
First let’s talk about the waterfall flow method used in this example. There are many ways to implement waterfall flow layout. For details, you can search on Baidu yourself, so I won’t go into details here. The waterfall flow implementation method in this article is a four-column layout (li*4). Each picture is a box (p>img p). After reading the data from the background, it is assigned to the elements in the box to determine the column with the smallest height at this time ( li), then add the box to the corresponding column (li), and then perform the next judgment, and so on until all data on this page is loaded.
Code part:
html css
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>瀑布流布局</title> <style type="text/css"> *{ margin: 0; padding: 0; } ul{ width: 1200px; margin: 0 auto; } ul li{ float: left; width: 250px; list-style: none; margin: 20px; } ul li p{ width: 250px; margin-bottom: 20px; padding: 10px; box-sizing: border-box; border-radius: 5px; box-shadow: 2px 2px 10px #919B9C; } ul li img{ width: 100%; margin-bottom: 10px; } ul li p{ font-family: "microsoft yahei"; font-size: 14px; } </style> <script src="ajax.js" type="text/javascript" charset="utf-8"></script> <script src="pubuliu.js" type="text/javascript" charset="utf-8"></script> </head> <body> <ul id="ul1"> <li></li> <li></li> <li></li> <li></li> </ul> </body> </html>
javascript part: ajax part and implementation part
/** * * @param {Object} method get和post方式 * @param {Object} url 文件路径 * @param {Object} data 页码 * @param {Object} success 成功的函数 */ function ajax(method, url, data, success) { var xhr = null; try { xhr = new XMLHttpRequest(); } catch (e) { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } if (method == 'get' && data) { url += '?' + data; } xhr.open(method,url,true); if (method == 'get') { xhr.send(); } else { xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); xhr.send(data); } xhr.onreadystatechange = function() { if ( xhr.readyState == 4 ) { if ( xhr.status == 200 ) { success && success(xhr.responseText); console.log(xhr.responseText); } else { alert('出错了,Err:' + xhr.status); } } } }
The ajax part is before The working principle of Ajax and the simple encapsulation of functions are modified. After understanding that, it is basically not difficult to read this. Compared with that one, this one has one more data parameter. Data is the page number used to read the data.
window.onload = function() { //获取界面节点 var ul = document.getElementById('ul1'); var li = document.getElementsByTagName('li'); var liLen = li.length; var page = 1; var bool = false; //调用接口获取数据 loadPage();//首次加载 /** * 加载页面的函数 */ function loadPage(){ ajax('get', 'getPics.php', 'cpage='+page, function(data) { //将数据库中获取的数据转换成数组形式,这里要根据数据库中的数据形式来写,这里我获取到的是json形式 var data = JSON.parse(data); //将数据写入到p中 for(var i = 0; i < data.length; i++) { var index = getShort(li);//查找最短的li //创建新的节点:p>img+p var p = document.createElement('p'); var img = document.createElement('img'); img.src = data[i].preview;//img获取图片地址 img.alt = "等着吧..." //根据宽高比计算img的高,为了防止未加载时高度太低影响最短Li的判断 img.style.height = data[i].height * (230 / data[i].width) + "px"; p.appendChild(img); var p = document.createElement('p'); p.innerHTML = data[i].title;//p获取图片标题 p.appendChild(p); //加入到最短的li中 li[index].appendChild(p); } bool = true;//加载完成设置开关,用于后面判断是否加载下一页 }); } window.onscroll = function (){ var index = getShort(li); var minLi = li[index]; var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; if(minLi.offsetHeight+minLi.offsetTop<scrollTop+document.documentElement.clientHeight){ //开关为开,即上一页加载完成,才能开始加载 if(bool){ bool = false; page++; loadPage(); } } } } /** * 获取数组中高度最小的索引 * @param {Object} li 数组 */ function getShort(li) { var index = 0; var liHeight = li[index].offsetHeight; for(var i = 0; i < li.length; i++) { if(li[i].offsetHeight < liHeight) { index = i; liHeight = li[i].offsetHeight; } } return index; }
The function of this part is mainly to dynamically write the generated p to the page, which includes modification of the box style and writing of data. The data is obtained from the server through the ajax function.
It should be noted that the operation of this instance depends on the server, so a simple server needs to be built locally. WampService can be used to quickly build it.
The following is the PHP code of our data source:
<?php header('Content-type:text/html; charset="utf-8"'); /* API: getPics.php 参数 cpage : 获取数据的页数 */ $cpage = isset($_GET['cpage']) ? $_GET['cpage'] : 1; $url = 'http://www.wookmark.com/api/json/popular?page=' . $cpage; $content = file_get_contents($url); $content = iconv('gbk', 'utf-8', $content); echo $content; ?>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of JSONP implementation principles and cases
Detailed explanation of the use of AJAX request queue
The above is the detailed content of Detailed explanation of the steps to implement waterfall flow layout with ajax (with code). 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

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

WeChat is one of the social media platforms in China that continuously launches new versions to provide a better user experience. Upgrading WeChat to the latest version is very important to keep in touch with family and colleagues, to stay in touch with friends, and to keep abreast of the latest developments. 1. Understand the features and improvements of the latest version. It is very important to understand the features and improvements of the latest version before upgrading WeChat. For performance improvements and bug fixes, you can learn about the various new features brought by the new version by checking the update notes on the WeChat official website or app store. 2. Check the current WeChat version We need to check the WeChat version currently installed on the mobile phone before upgrading WeChat. Click to open the WeChat application "Me" and then select the menu "About" where you can see the current WeChat version number. 3. Open the app

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

Having issues with the Shazam app on iPhone? Shazam helps you find songs by listening to them. However, if Shazam isn't working properly or doesn't recognize the song, you'll have to troubleshoot it manually. Repairing the Shazam app won't take long. So, without wasting any more time, follow the steps below to resolve issues with Shazam app. Fix 1 – Disable Bold Text Feature Bold text on iPhone may be the reason why Shazam is not working properly. Step 1 – You can only do this from your iPhone settings. So, open it. Step 2 – Next, open the “Display & Brightness” settings there. Step 3 – If you find that “Bold Text” is enabled

Windows 11, as the latest operating system launched by Microsoft, is deeply loved by users. In the process of using Windows 11, sometimes we need to obtain system administrator rights in order to perform some operations that require permissions. Next, we will introduce in detail the steps to obtain system administrator rights in Windows 11. The first step is to click "Start Menu". You can see the Windows icon in the lower left corner. Click the icon to open the "Start Menu". In the second step, find and click "

Screenshot feature not working on your iPhone? Taking a screenshot is very easy as you just need to hold down the Volume Up button and the Power button at the same time to grab your phone screen. However, there are other ways to capture frames on the device. Fix 1 – Using Assistive Touch Take a screenshot using the Assistive Touch feature. Step 1 – Go to your phone settings. Step 2 – Next, tap to open Accessibility settings. Step 3 – Open Touch settings. Step 4 – Next, open the Assistive Touch settings. Step 5 – Turn on Assistive Touch on your phone. Step 6 – Open “Customize Top Menu” to access it. Step 7 – Now you just need to link any of these functions to your screen capture. So click on the first

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

If you don't have control over the zoom level in Safari, getting things done can be tricky. So if Safari looks zoomed out, that might be a problem for you. Here are a few ways you can fix this minor zoom issue in Safari. 1. Cursor magnification: Select "Display" > "Cursor magnification" in the Safari menu bar. This will make the cursor more visible on the screen, making it easier to control. 2. Move the mouse: This may sound simple, but sometimes just moving the mouse to another location on the screen may automatically return it to normal size. 3. Use Keyboard Shortcuts Fix 1 – Reset Zoom Level You can control the zoom level directly from the Safari browser. Step 1 – When you are in Safari
