当前位置:网站首页>Long list optimized virtual scrolling
Long list optimized virtual scrolling
2022-07-05 14:36:00 【Forgive me for not being free and easy enough】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
ul.container {
position: relative;
width: 500px;
max-height: 500px;
/* Limit the size of the container display area */
margin: 0;
padding: 0;
border: 1px solid #000;
overflow-y: auto;
list-style-type: none;
}
ul.container li {
position: absolute;
width: 100%;
height: 50px;
color: white;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<ul class="container"></ul>
<script>
const dataList = Array.from({
length: 1000 }).map((item, index) => index); // Generate 1000 pieces of data
const $list = document.querySelector(".container")
const size = 20 // Visual area 10 strip , But you need to scroll Take the number of pieces each time 20
createItem(0) // The default from the 0 Start (i)
$list.setAttribute("style", `height: ${
50 * dataList.length}px;`); // Set container height
function handleScroll(e) {
// Calculations should show those
const i = Math.floor(e.target.scrollTop / 50);
createItem(i);
}
function createItem(i) {
$list.innerHTML = dataList.slice(i, i + size).map(
item =>
`<li style="top: ${
item * 50}px; background: ${
createHexColor()};">${
item}</li>`)
.join("");
}
$list.onscroll = throttle(handleScroll, 20);
function throttle(fn, delay) {
// Events triggered by scrolling
let lastTime = 0;
return function () {
const nowTime = +new Date();
if (nowTime - lastTime > delay) {
fn.apply(this, arguments);
lastTime = nowTime;
}
}
}
function createHexColor() {
const colorStr = "6789abcdef",
len = colorStr.length;
let colorVal = "#";
for (let i = 0; i < 6; i++) {
colorVal += colorStr[Math.floor(Math.random() * len)];
}
return colorVal
}
</script>
</body>
</html>
边栏推荐
猜你喜欢
FR练习题目---综合题
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Sharing the 12 most commonly used regular expressions can solve most of your problems
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
面试突击62:group by 有哪些注意事项?
选择排序和冒泡排序
CPU设计相关笔记
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
LeetCode_ 2 (add two numbers)
Thymeleaf 模板的创建与使用
随机推荐
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
动态规划
强联通分量
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
外盘入金都不是对公转吗,那怎么保障安全?
LeetCode_ 2 (add two numbers)
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
【C 题集】of Ⅷ
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
04_ Use of solrj7.3 of solr7.3
03_ Dataimport of Solr
矩阵链乘 - 动态规划实例
How can non-technical departments participate in Devops?
How to choose the appropriate certificate brand when applying for code signing certificate?
LeetCode_ 69 (square root of x)
【NVMe2.0b 14-9】NVMe SR-IOV
【招聘岗位】软件工程师(全栈)- 公共安全方向
Catch all asynchronous artifact completable future
裁员下的上海