How to create an infinite scrolling news list using HTML, CSS and jQuery

WBOY
Release: 2023-10-24 11:00:18
Original
1270 people have browsed it

How to create an infinite scrolling news list using HTML, CSS and jQuery

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:

    无限滚动新闻列表  
正在加载更多...
Copy after login

Next, we need to set up some basic CSS styles for this news list. Create a file calledstyle.cssand 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; }
Copy after login

In this example, we set up the news list container#news-listThe height is 400px and uses theoverflow-y: scroll;attribute to achieve vertical scrolling. The style of the news list uses unordered listuland 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.jsand 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(); } }); });
Copy after login

In this code, we first define a variablepageto represent the current load The number of pages, and a variableloadingindicating whether data is being loaded.loadDataIn 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 theloadingflag tofalse.

After completing the above code, we need to create a server-side interface file namednews.phpto simulate back-end data. Here is a simple example:

新闻标题 ' . ($start + $i) . ''; } echo implode('', $data);
Copy after login

In this example, we use$pageto get the number of pages currently requested and$pageSizeto 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.jsand the style filestyle.cssand script filescript.js## required for the page. # Place in the same directory as the server-side interface filenews.php.

The above are the basic steps and code examples to create an infinite scrolling news list using HTML, CSS and jQuery. You can adjust the style according to actual needs and modify the server-side interface file to load real news data. I hope this article is helpful to you, and I wish you to implement an elegant infinite scrolling news list!

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!