当前位置:网站首页>长列表优化虚拟滚动
长列表优化虚拟滚动
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>
边栏推荐
- Thymeleaf 常用函数
- 网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
- 有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- Share 20 strange JS expressions and see how many correct answers you can get
- mysql8.0JSON_CONTAINS的使用说明
- Penetration testing methodology
- How does redis implement multiple zones?
- Thymeleaf th:with use of local variables
- R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
猜你喜欢
FR练习题目---简单题
Introduction, installation, introduction and detailed introduction to postman!
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
周大福践行「百周年承诺」,真诚服务推动绿色环保
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
超级哇塞的快排,你值得学会!
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
Section - left closed right open
安装配置Jenkins
Security analysis of Web Architecture
随机推荐
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
注意!软件供应链安全挑战持续升级
How to call the function mode of one hand and one machine
dynamic programming
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
[learning notes] stage test 1
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
C language -- structure and function
Catch all asynchronous artifact completable future
FR练习题目---简单题
Why do mechanical engineers I know complain about low wages?
FR练习题目---综合题
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
不相交集
R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
be careful! Software supply chain security challenges continue to escalate