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

WBOY
WBOYOriginal
2023-10-24 11:00:181379browse

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>

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