js implements loading more function instances
When a front-end page of the project displays purchased products, it is required to be able to pull down to load more. Regarding how to implement the "load more" function, there are plug-ins available online, such as the well-known pull-up to load more and pull-down to refresh functions implemented using iscroll.js.
But it is very troublesome to use in practice. Since it is a third-party plug-in, it has to be used according to the method defined by the other party, which always feels very uncomfortable to use. In addition, iscroll.js itself does not integrate more functions and needs to be expanded by itself. If you want to continue using iscroll.js to load more functions, you can check out the link given above.
The h5 project needs to implement a simple paging function. Since it is a mobile terminal, it would be better to consider using "Load More" instead of turning pages on the PC side.
Load more based on buttons
The simplest way is to give a load more button. If there is still data, click it to load more, and continue to pull a few pieces of data; until there is no more data, hide the load more button.
The effect is as follows:
Page html:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Load more button style: loadmore.css:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Load more js code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
There is not much code here. Among them, getData(pageStart, pageSize) is the business logic code, which is responsible for pulling data from the server. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
It’s relatively simple.
Loading more based on scroll events
Above we implemented loading more by clicking the button, and the overall process is relatively simple. Here, I provide another way to load more: based on scroll events.
Posted the code directly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
It can be seen that the code has not changed much. It mainly depends on the judgment conditions in the core code: when scrolling to 100 pixels above the bottom, new content is loaded.
Business logic getData(pageStart, pageSize) only needs to change the logic in if ( (offset + size) >= sum) to:
1 2 3 |
|
.
Of course, there are still areas that need to be optimized, such as: How to prevent the server from scrolling too fast and causing multiple requests before the server has time to respond?
Comprehensive example
Through the above example, it is obvious that the second one is better, no need to click. But there is a problem with the second method:
If you set the page size to display 2 or 3 items each time (size=2), and the total records are 20, you will find that you cannot trigger the logic of scrolling down to load more. At this time it would be nice to have a click button to load more.
Therefore, we can combine the above two methods:
By default, scroll events are used to load more. When the display number is too small to trigger the logic of scrolling down to load more, use Load More Clicks event.
Here, I simply abstracted the behavior of loading more and wrote a simple plug-in:
loadmore.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
How to use it? It's simple:
1 2 3 4 5 |
|
The first parameter is the callback function, which is our business logic. I posted the final modified business logic method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|

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

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
