在Javascript中使用文件数据进行无限滚动

问题描述 投票:0回答:1

我想用纯Javascript做无限滚动。我看到了几个教程,但所有的教程都是从某个api中随机获取一些数据,我理解教程中的代码,但我不知道如何按顺序获取数据,而不是随机的。我理解教程中的代码,但我不知道如何按顺序而不是随机地获取数据。

我想做一些类似这里的东西。https:/codepen.ioFlorinPop17penRwwvKYJ。 但我想使用我本地文件中的数据。让我们假设它是data.js,有这样的代码。

data = [{}, {}] 

所以它是一个对象数组 我们假设对象的内容是这样的: https:/jsonplaceholder.typicode.composts。

如何将这个codepen的代码改成按顺序一个个显示帖子?我想,函数getPost应该有参数 "id",每次调用这个函数时,参数应该加1?但是怎么做呢?或者我应该在data.js中迭代,每次迭代时检查用户是否滚动到底部?

javascript arrays infinite-scroll
1个回答
1
投票

你只需要改变 getPost() 函数来使用您的内联 blog_data 其中包含所有可用的员额。当前的偏移量被保存在一个全局变量 post_offset 每增加 getPost() 调用,这样订单就会保持不变,不会多次显示帖子。

// all the blog entries that are available
const blog_data = [{
  title: "Blog Entry 1",
  body: "This is the example body text for entry 1."
},{
  title: "This is number two",
  body: "Also blog entry number 2 has some content."
},{
  title: "Blog entry three",
  body: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."
},{
  title: "Blog entry four",
  body: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."
},{
  title: "Blog entry five",
  body: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."
},{
  title: "Blog entry six",
  body: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."
}];

const container = document.getElementById('container');
const loading = document.querySelector('.loading');
let post_offset = 0;

getPost();
getPost();
getPost();

window.addEventListener('scroll', () => {
  const { scrollTop, scrollHeight, clientHeight } = document.documentElement;

  if(clientHeight + scrollTop >= scrollHeight - 5) {
    // show the loading animation
    showLoading();
  }
});

function showLoading() {
  if(post_offset < blog_data.length){
    loading.classList.add('show');

    // load more data
    setTimeout(getPost, 1000)
  }
  else{
    // end has been reached, no more posts available
  }
}

async function getPost() {
  if(post_offset < blog_data.length){
    addDataToDOM(blog_data[post_offset]);
    post_offset++;
  }
}

function addDataToDOM(data) {
  const postElement = document.createElement('div');
  postElement.classList.add('blog-post');
  postElement.innerHTML = `
    <h2 class="title">${data.title}</h2>
    <p class="text">${data.body}</p>
  `;
  container.appendChild(postElement);

  loading.classList.remove('show');
}
@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,600&display=swap');

* {
  box-sizing: border-box;
}

body {
  background-color: #fafafa;
  font-family: 'Open Sans', sans-serif;
  padding-bottom: 100px;
}

.container {
  margin: 0 auto;
  max-width: 600px;
}

.blog-post {
  background-color: #fff;
  box-shadow: 0px 1px 2px rgba(50, 50, 50, .1), 0px 2px 4px rgba(60, 60, 60, 0.1);
  border-radius: 4px;
  padding: 40px;
  margin: 20px 0;
}

.title {  
  margin: 0;  
}

.text {
  color: #555;
  margin: 20px 0;
}

.loading {
  opacity: 0;
  display: flex;
  position: fixed;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
  transition: opacity .3s ease-in;
}

.loading.show {
  opacity: 1;
}

.ball {
  background-color: #777;
  border-radius: 50%;
  margin: 5px;
  height: 10px;
  width: 10px;
  animation: jump .5s ease-in infinite;
}

.ball:nth-of-type(2) {
  animation-delay: 0.1s;
}

.ball:nth-of-type(3) {
  animation-delay: 0.2s;
}

@keyframes jump {
  0%, 100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}
<div class="container" id="container">
  <h1>Blog Posts</h1>
</div>

<div class="loading">
  <div class="ball"></div>
  <div class="ball"></div>
  <div class="ball"></div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.