当前位置:网站首页>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>
边栏推荐
- 强联通分量
- Security analysis of Web Architecture
- How can non-technical departments participate in Devops?
- PostgreSQL 13 installation
- 两个BI开发,3000多张报表?如何做的到?
- Lepton 无损压缩原理及性能分析
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- The forked VM terminated without saying properly goodbye
- 手写promise与async await
- 分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
猜你喜欢
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
分享 12 个最常用的正则表达式,能解决你大部分问题
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
实现一个博客系统----使用模板引擎技术
How to protect user privacy without password authentication?
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
安装配置Jenkins
Redis如何实现多可用区?
Topology可视化绘图引擎
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
随机推荐
Niuke: intercepting missiles
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
Isn't it right to put money into the external market? How can we ensure safety?
做自媒体视频二次剪辑,怎样剪辑不算侵权
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
mysql8.0JSON_ Instructions for using contains
选择排序和冒泡排序
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
How does redis implement multiple zones?
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
Mysql database installation tutorial under Linux
C language -- structure and function
webRTC SDP mslabel lable
Lepton 无损压缩原理及性能分析
Penetration testing methodology
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management