当前位置:网站首页>长列表优化虚拟滚动
长列表优化虚拟滚动
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>
边栏推荐
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- The simplest way to open more functions without certificates
- MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
- Principle and performance analysis of lepton lossless compression
- LeetCode_ 67 (binary sum)
- 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
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- The function of qualifier in C language
- PostgreSQL 13 installation
猜你喜欢

Thymeleaf 模板的创建与使用

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器

无密码身份验证如何保障用户隐私安全?

Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection

选择排序和冒泡排序

Pointer operation - C language

Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute

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

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
随机推荐
C language -- structure and function
R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
区间 - 左闭右开
How does redis implement multiple zones?
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
The simplest way to open more functions without certificates
安装配置Jenkins
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
乌卡时代下,企业供应链管理体系的应对策略
The function of qualifier in C language
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
Matrix chain multiplication dynamic programming example
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
启牛证券账户怎么开通,开户安全吗?
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
Thymeleaf 常用函數
anaconda使用中科大源
想进阿里必须啃透的12道MySQL面试题