How to create an infinite scrolling news list using HTML, CSS and jQuery
In web development, infinite scrolling is a common interaction technique, especially suitable for Pages such as news lists that need to load a large amount of data. This article will introduce how to use HTML, CSS and jQuery to implement an infinite scrolling news list, and provide specific code examples.
First, we need a basic HTML structure to display the news list. Here is a simple example:
Next, we need to set up some basic CSS styles for this news list. Create a file calledstyle.css
and add the following code:
#news-list { overflow-y: scroll; height: 400px; } ul { list-style: none; padding: 0; margin: 0; } li { padding: 10px; border-bottom: 1px solid #ccc; } #loading-indicator { text-align: center; padding: 10px; color: #999; }
In this example, we set up the news list container#news-list
The height is 400px and uses theoverflow-y: scroll;
attribute to achieve vertical scrolling. The style of the news list uses unordered listul
and list itemsli
, and adds a gray bottom line to the list items. The style of loading more prompt information is set in#loading-indicator
.
Okay, now let’s write JavaScript code and use jQuery to achieve the infinite scrolling effect. Create a file namedscript.js
and add the following code:
$(document).ready(function() { var page = 1; var loading = false; function loadData() { if (loading) return; loading = true; $('#loading-indicator').show(); // 发起异步请求加载数据 $.ajax({ url: 'news.php', // 替换为加载数据的API地址 type: 'GET', data: { page: page }, success: function(data) { // 数据加载成功后的处理 if (data.length > 0) { $('#news-list ul').append(data); page++; } loading = false; $('#loading-indicator').hide(); } }); } // 初始化加载第一页的数据 loadData(); // 当滚动到底部时触发加载下一页数据 $('#news-list').scroll(function() { var distance = $('#news-list ul').outerHeight() - $('#news-list').scrollTop() - $('#news-list').height(); if (distance < 50) { loadData(); } }); });
In this code, we first define a variablepage
to represent the current load The number of pages, and a variableloading
indicating whether data is being loaded.loadData
In the function, we determine whether there is data currently being loaded, and if so, return directly, otherwise the loading prompt information is displayed, and an asynchronous request is sent to obtain the news data. After the request is successful, the news data is appended to the news list and the page number is updated. Finally, hide the loading prompt and set theloading
flag tofalse
.
After completing the above code, we need to create a server-side interface file namednews.php
to simulate back-end data. Here is a simple example:
新闻标题 ' . ($start + $i) . ''; } echo implode('', $data);
In this example, we use$page
to get the number of pages currently requested and$pageSize
to set each page The number of news items displayed. Then, simulated news data is generated through a simple loop and returned to the front end.
Finally, download the jQuery filejquery.js
and the style filestyle.css
and script filescript.js## required for the page. # Place in the same directory as the server-side interface file
news.php.
The above is the detailed content of How to create an infinite scrolling news list using HTML, CSS and jQuery. For more information, please follow other related articles on the PHP Chinese website!