Home Web Front-end JS Tutorial 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

Oct 24, 2023 am 11:00 AM
css jquery html infinite scroll

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:

<!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>
Copy after login

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;
}
Copy after login

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();
    }
  });
});
Copy after login

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);
Copy after login

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 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles