javascript - div 中的简单 Intifite Scroll (React)
问题描述
我有一个数组,其中包含我想在高度为 150px 的 div 中呈现的元素,并具有无限滚动。
我查看了 Intersection Observer,但我找不到任何可以使用的东西,因为我的用例非常简单。我要引入无限滚动的元素如下所示:
const data = ["honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota"]
<Container>
<ul>
{data.map((d, index) => (
<li key={`${index}`}>
<div>
<span>some text</span>
<span>some text</span>
<span>some text</span>
</div>
</li>
))}
</ul>
</Container>
我想在 Container div 中有无限滚动。
解决方案
(function () {
const containerEl = document.querySelector('container');
const itemsEl = document.querySelector('.items');
const loaderEl = document.querySelector('.loader');
const items = ["honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab"];
const getItems = (page,limit) => {
const data = {'data':items.slice((page - 1)*limit,page*limit), 'total':items.length}
return data;
}
const showItems = (items) => {
items.forEach(item => {
const itemEl = document.createElement('item');
itemEl.classList.add('item');
itemEl.innerHTML = `${item} `;
itemsEl.appendChild(itemEl);
});
};
const hasMore = (page, limit, total) => {
const startIndex = (page - 1) * limit + 1;
return total === 0 || startIndex < total;
};
const loadItem = (page, limit) => {
if (hasMore(page, limit, total)) {
const fetched=getItems(page, limit);
showItems(fetched['data']);
total = fetched['total'];
}
};
let currentPage = 1;
let limit = 6;
let total = 0;
containerEl.addEventListener('scroll', () => {
const {
scrollTop,
scrollHeight,
clientHeight
} = containerEl;
if (scrollTop + clientHeight >= scrollHeight - 5 &&
hasMore(currentPage, limit, total)) {
currentPage++;
loadItem(currentPage, limit);
}
}, {
passive: true
});
loadItem(currentPage, limit);
})();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
container {
margin: 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height:150px;
background:#999999;
overflow:scroll;
}
.items {
display: flex;
flex-direction: column;
justify-content: center;
}
.item {
position: relative;
font-size: 20px;
line-height: 1.7em;
}
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Infinite Scroll</title>
</head>
<body>
<Container>
<div class="items"></div>
</Container>
</body>
</html>
使用这个不错的教程: JavaScript Infinite Scroll
推荐阅读
- r - rowMeans 不适用于我的数据 - 所有向量都是数字
- redis - 使用内联 redis-cli 命令取消注册 RedisGears 注册会导致“ERR 未知命令 `RG_UNREGISTER`”
- python - AttributeError:模块“delivery.models.order_timelocation”没有属性“_meta”
- android - 错误状态:DocumentSnapshotPlatform 中不存在字段。(StreamBuilder 有问题)
- kubernetes - 防止容器内的OOM
- javascript - 我的网站在 chrome 上的欺骗性网站提前警告
- python - 如何在不按 kivymd 按钮的情况下自动从 MDTextfield 获取数据
- r - Legend position based on coordinates in ggplot2
- python - excel文件中的每两列被分成多个csv文件
- python-3.x - 发生异常:IndexError list index out of range, line 5, in module file_path - sys.argv[1] Python & PyPDF2