Javascript-无限滚动JSON数组

Javascript - Infinite scroll JSON array?

本文关键字:数组 JSON 滚动 无限 Javascript-      更新时间:2023-09-26

我有这样的JavaScript:

items.forEach(function (item, index, arr) {
                console.log(item.price);
                var message = 'BitSkins Price: $' + item.bprice + '';
                if (item.price != null) {
                    if (item.bprice == '') {
                        message = 'Item never sold on BitSkins!';
                    }
                    if (item.name != 'Operation Phoenix Case Key' && item.name != 'CS:GO Case Key' && item.name != 'Winter Offensive Case Key' && item.name != 'Revolver Case Key' && item.name != 'Operation Vanguard Case Key' && item.name != 'Operation Wildfire Case Key' && item.name != 'Shadow Case Key' && item.name != 'Operation Breakout Case Key' && item.name != 'Chroma Case Key' && item.name != 'Huntsman Case Key' && item.name != 'Falchion Case Key' && item.name != 'Chroma 2 Case Key') {
                        $("#inventory").html($("#inventory").html() + "<li class='col 2' style='padding:8px;font-weight:bold;font-size:16px'><div class='card item-card waves-effect waves-light' style='margin:0%;min-height:295px;width:245.438px;border-radius: 15px;' id='" + item.id + "'><div class='iteam' style='text-decoration: underline;text-align: left'>" + item.name + "</div><div class='condition' style='text-align: left;text-size:13px'>" + item.condition + "</div><div class='center-align' style='padding:6%'><img title='"" + item.originalname + "'" draggable='false' src='https://steamcommunity-a.akamaihd.net/economy/image/" + item.iconurl + "/200fx200'><div class 'floatvalue'>Float: 0.11503319442272186<div class='bitskinscomp' style='font-weight: normal;font-size:12px'>" + message + "</div><div class='buyer-price center-align'>$" + numberWithCommas(item.price) + "</li></div></div>");
                    }
                }
            });

它将数组中的每个CCD_ 1添加到CCD_。items数组包含JSON,可以是1000不同的项。如何在该JavaScript上添加infinite scroll?示例:它会显示前50个项目,然后如果你再滚动50个。此外,按价格排序(我已经在代码中得到了)。

你可以很容易地这样做:

var perPage = 50;
function paginate(items, page) {
  var start = perPage * page;
  return items.slice(start, start + perPage);
}
function renderItems(pageItems) {
  pageItems.forEach(function (item, index, arr) {
    var message = 'BitSkins Price: $' + item.bprice + '';
    if (item.price != null) {
      if (item.bprice == '') {
        message = 'Item never sold on BitSkins!';
      }
      if (item.name != 'Operation Phoenix Case Key' && item.name != 'CS:GO Case Key' && item.name != 'Winter Offensive Case Key' && item.name != 'Revolver Case Key' && item.name != 'Operation Vanguard Case Key' && item.name != 'Operation Wildfire Case Key' && item.name != 'Shadow Case Key' && item.name != 'Operation Breakout Case Key' && item.name != 'Chroma Case Key' && item.name != 'Huntsman Case Key' && item.name != 'Falchion Case Key' && item.name != 'Chroma 2 Case Key') {
        $("#inventory").append("<li class='col 2' style='padding:8px;font-weight:bold;font-size:16px'><div class='card item-card waves-effect waves-light' style='margin:0%;min-height:295px;width:245.438px;border-radius: 15px;' id='" + item.id + "'><div class='iteam' style='text-decoration: underline;text-align: left'>" + item.name + "</div><div class='condition' style='text-align: left;text-size:13px'>" + item.condition + "</div><div class='center-align' style='padding:6%'><img title='"" + item.originalname + "'" draggable='false' src='https://steamcommunity-a.akamaihd.net/economy/image/" + item.iconurl + "/200fx200'><div class 'floatvalue'>Float: 0.11503319442272186<div class='bitskinscomp' style='font-weight: normal;font-size:12px'>" + message + "</div><div class='buyer-price center-align'>$" + numberWithCommas(item.price) + "</li></div></div>");
      }
    }
  });
}
$(document).ready(function() {
  var win = $(window);
  var page = 0;
  renderItems(paginate(items, page));
  // Each time the user scrolls
  win.scroll(function() {
    // End of the document reached?
    if ($(document).height() - win.height() == win.scrollTop()) {
      page++;
      renderItems(paginate(items, page));
    }
  });
});

或者使用jQuery endlessScroll插件

$(document).ready(function() {
  $(window).endlessScroll({
    inflowPixels: 300,
    callback: function() {
      //append new items to your list
    }
  });
});

不如写一个小函数,检查滚动位置并启动ajax调用以获取更多数据,或者只从json对象中获取下一个数据槽并将其绑定到HTML。如下所示:

$(window).scroll(function() {
    if($(window).scrollTop() == $(document).height() - $(window).height()) {
           // ajax call or some other logic to show data here
    }
});

或者您可以使用作为众多插件中的一个,我使用Waypoint做同样的事情。

尝试使用其他变量,如当前页面(如果您有1000个项目,每个项目中显示50个,则最多显示20个页面)、页面上的项目数、开始索引和结束索引。

假设:

var currPage = 0; //(First page)

var itemsInPage = 50; //NUMBER OF ITEMS IN A PAGE

然后,对于每个滚动,计算startItem索引和endItem索引。

item0

endItem = startItem + itemsInPage; //AND THIS IS 50

forEach循环中,选中if( index >= startItem && index < endItem )并仅显示这些项目。

您必须在每次滚动后增加currPage,并在forEach循环开始时添加:

if( currPage > Math.ceil( items.length/itemsInPage ) ) currPage = 1;

(使用ceil进行四舍五入,因为如果"items"的长度不能完全被"itemsinpage"整除,那么它们加起来就是一个额外的页面)

如果您可以使用第三方,请查看此处的无限ajax滚动。

或者在类似的问题中解释,使用JQuery Waypoint插件