


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:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>无限滚动新闻列表</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="news-list"> <ul> <!-- 这里将显示动态加载的新闻 --> </ul> <div id="loading-indicator">正在加载更多...</div> </div> <script src="jquery.js"></script> <script src="script.js"></script> </body> </html>
Next, we need to set up some basic CSS styles for this news list. Create a file called style.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 the overflow-y: scroll;
attribute to achieve vertical scrolling. The style of the news list uses unordered list ul
and list items li
, 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 named script.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 variable page
to represent the current load The number of pages, and a variable loading
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 the loading
flag to false
.
After completing the above code, we need to create a server-side interface file named news.php
to simulate back-end data. Here is a simple example:
<?php $page = $_GET['page']; $pageSize = 10; $start = ($page - 1) * $pageSize; $data = []; // 模拟加载数据 for ($i = 1; $i <= $pageSize; $i++) { $data[] = '<li>新闻标题 ' . ($start + $i) . '</li>'; } 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 file jquery.js
and the style file style.css
and script file script.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!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
