当前位置:网站首页>长列表优化虚拟滚动
长列表优化虚拟滚动
2022-07-05 14:32:00 【原谅我不够洒脱】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
ul.container {
position: relative;
width: 500px;
max-height: 500px;
/* 限定容器展示区域的大小 */
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); // 生成一千条数据
const $list = document.querySelector(".container")
const size = 20 // 可视区域10条,但是需要滚动 每次的条数就取20
createItem(0) // 默认从0开始(i)
$list.setAttribute("style", `height: ${
50 * dataList.length}px;`); // 设置容器高度
function handleScroll(e) {
// 计算应该展示那些
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) {
// 滚动触发的事件
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>
边栏推荐
- 申请代码签名证书时如何选择合适的证书品牌?
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
- Discussion on memset assignment
- Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
- How to call the function mode of one hand and one machine
- 启牛证券账户怎么开通,开户安全吗?
- Topology visual drawing engine
- 强联通分量
- R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
- Strong connection component
猜你喜欢

世界环境日 | 周大福用心服务推动减碳环保

如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴

How can non-technical departments participate in Devops?

How does redis implement multiple zones?

PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用

【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)

Sharing the 12 most commonly used regular expressions can solve most of your problems

分享 12 个最常用的正则表达式,能解决你大部分问题

APR protocol and defense

无密码身份验证如何保障用户隐私安全?
随机推荐
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
Structure - C language
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Postgresql 13 安装
How to deeply understand the design idea of "finite state machine"?
申请代码签名证书时如何选择合适的证书品牌?
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
Thymeleaf th:with局部变量的使用
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
循环不变式
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
The forked VM terminated without saying properly goodbye
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
Time to calculate cron expression based on cronsequencegenerator
Principle and performance analysis of lepton lossless compression
Two policemen were shot dead in a "safety accident" in Philadelphia, USA
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)