当前位置:网站首页>长列表优化虚拟滚动
长列表优化虚拟滚动
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>
边栏推荐
- 世界环境日 | 周大福用心服务推动减碳环保
- MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
- leetcode:881. 救生艇
- LeetCode_ 3 (longest substring without repeated characters)
- 总量分析 核算方法和势方法 - 分摊分析
- Webrtc learning (II)
- [summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
- How to choose the appropriate certificate brand when applying for code signing certificate?
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
猜你喜欢

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)

Principle and performance analysis of lepton lossless compression

How to deeply understand the design idea of "finite state machine"?

区间 - 左闭右开

Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation

Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million

FR练习题目---简单题

Introduction, installation, introduction and detailed introduction to postman!
随机推荐
Thymeleaf 使用后台自定义工具类处理文本
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
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
区间 - 左闭右开
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
实现一个博客系统----使用模板引擎技术
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
LeetCode_ 69 (square root of x)
Loop invariant
Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
非技术部门,如何参与 DevOps?
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
在Pytorch中使用Tensorboard可视化训练过程
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Section - left closed right open