当前位置: 首页 > news >正文

微信里借钱的小程序seo交流网

微信里借钱的小程序,seo交流网,wordpress仪表盘加载很慢,服务器怎么建网站虚拟列表 - Vue3实现一个可动态改变高度的虚拟滚动列表 前言 在开发中经常遇到大量的渲染列表数据问题,往往我们就只是简单地遍历渲染,没有过多地去关注是否会存在性能问题,这导致如果数据量较大的时候,比如上万条数据&#xff…

虚拟列表 - Vue3实现一个可动态改变高度的虚拟滚动列表

前言

在开发中经常遇到大量的渲染列表数据问题,往往我们就只是简单地遍历渲染,没有过多地去关注是否会存在性能问题,这导致如果数据量较大的时候,比如上万条数据,将会在dom中渲染上万个节点,这将加大浏览器的开销,可能会导致页面卡顿,加载慢等性能问题。因此,在渲染大量数据时,可以选择使用虚拟列表,只渲染用户可视区域内的dom节点。该组件已开源上传npm,可以直接安装使用,Git地址在文尾。

虚拟列表实现原理

每条固定高度

1、通过传入组件的每条数据的高度,计算整个列表的高度,从而得到滚动列表的总高,并将总高赋值给列表。
2、监听滚动事件,监听外层容器的滚动事件,并确定可视区域内起止数据在总数据的索引值,这可以通过scrollTop来实现。
3、设置数据对应的元素,为每条数据设置一个绝对定位,其中top等于索引值乘以每条数据的高度。
4、考虑缓冲条数,为了避免滑动过快产生空白,可以设置缓冲条数。具体来说,如果滚动到底部,可以只显示最后N条数据,如果滚动到上部,可以只显示前N条数据。
这样,就可以实现一个固定高度的虚拟列表。

每条动态高度

原理和固定高度基本一致,差别在于,用户可以预先定义每条数据的高度,在渲染时再动态获取每一条数据的实际高度,从而重新计算滚动列表的总体高度。

主要代码实现

模板部分

showItemList循环可视区域内的数据+缓存区的数据

<template><div class="virtual-wrap" ref="virtualWrap" :style="{width: width + 'px',height: height + 'px',}" @scroll="scrollHandle"><div class="virtual-content" :style="{height: totalEstimatedHeight +'px'}"><list-item v-for="(item,index) in showItemList" :key="item.dataIndex+index" :index="item.dataIndex" :data="item.data" :style="item.style"@onSizeChange="sizeChangeHandle"><template #slot-scope="slotProps"><slot name="slot-scope" :slotProps="slotProps"></slot></template></list-item></div></div>
</template>
获取需要渲染的数据

通过可视区域内的开始和结束索引,获取需要渲染的列表数据。

const getCurrentChildren = () => {//重新计算高度estimatedHeight(props.itemEstimatedSize,props.itemCount)const [startIndex, endIndex] = getRangeToRender(props, scrollOffset.value)const items = [];for (let i = startIndex; i <= endIndex; i++) {const item = getItemMetaData(i);const itemStyle = {position: 'absolute',height: item.size + 'px',width: '100%',top: item.offset + 'px',};items.push({style: itemStyle,data: props.data[i],dataIndex:i});}showItemList.value = items;
}
获取开始和结束索引
const getRangeToRender = (props: any, scrollOffset: any) => {const { itemCount } = props;const startIndex = getStartIndex(props, scrollOffset);const endIndex = getEndIndex(props, startIndex + props.buffCount);return [Math.max(0, startIndex -1),Math.min(itemCount - 1, endIndex ),];
};const getStartIndex = (props: any, scrollOffset: number) => {const { itemCount } = props;let index = 0;while (true) {const currentOffset = getItemMetaData(index).offset;if (currentOffset >= scrollOffset) return index;if (index >= itemCount) return itemCount;index++}
}const getEndIndex = (props: any, startIndex: number) => {const { height, itemCount } = props;// 获取可视区内开始的项const startItem = getItemMetaData(startIndex);// 可视区内最大的offset值const maxOffset = Number(startItem.offset) + Number(height);// 开始项的下一项的offset,之后不断累加此offset,知道等于或超过最大offset,就是找到结束索引了let offset = Number(startItem.offset) + startItem.size;// 结束索引let endIndex = startIndex;// 累加offsetwhile (offset <= maxOffset && endIndex < (itemCount - 1)) {endIndex++;const currentItem = getItemMetaData(endIndex);offset += currentItem.size;}// 更新已计算的项的索引值measuredData.lastMeasuredItemIndex = endIndex;return endIndex;
};
动态计算节点高度

const estimatedHeight = (defaultEstimatedItemSize = 50, itemCount: number) => {let measuredHeight = 0;const { measuredDataMap, lastMeasuredItemIndex } = measuredData;// 计算已经获取过真实高度的项的高度之和if (lastMeasuredItemIndex >= 0) {const lastMeasuredItem = measuredDataMap[lastMeasuredItemIndex];measuredHeight = lastMeasuredItem.offset + lastMeasuredItem.size;}// 未计算过真实高度的项数const unMeasuredItemsCount = itemCount - measuredData.lastMeasuredItemIndex - 1;// 预测总高度totalEstimatedHeight.value = measuredHeight + unMeasuredItemsCount * defaultEstimatedItemSize;
}

子组件实现

1、通过ResizeObserver在子节点高度变化时触发父组件的方法,重新计算整体高度。
2、通过插槽将每条数据动态插入到列表中。

<template><div :style="style" ref="domRef"><slot name="slot-scope" :data="data"></slot></div>
</template>
<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue'const emit = defineEmits(['onSizeChange']);const props = defineProps({style: {type: Object,default: () => { }},data: {type: Object,default: () => { }},index: {type: Number,default: 0}
})const domRef = ref<any>(null);
const resizeObserver:any = null;onMounted(() => {const domNode = domRef.value.children[0];emit("onSizeChange", props.index, domNode);const resizeObserver = new ResizeObserver(() => {emit("onSizeChange", props.index, domNode);});resizeObserver.observe(domNode);
})onUnmounted(() => {if (resizeObserver) {resizeObserver?.unobserve(domRef.value.children[0]);}
})
</script>

组件使用

npm install @fcli/vue-virtually-list --save-dev 来安装在项目中使用
import VueVirtuallyList from '@fcli/vue-virtually-list';
const app=createApp(App)
app.use(VueVirtuallyList);

示例:


<div class="content"><vue-virtually-list :data="list" :height="400" :width="600" :itemCount="1000" :itemEstimatedSize="20" :buffCount="50"><template #slot-scope="{slotProps}"><div class="li">{{ slotProps.data.text }}</div></template></vue-virtually-list>
</div>
属性属性名称类型可选值
data列表数据Array[]
height虚拟容器的高度number0
width虚拟容器的宽度number0
itemCount滚动列表的条数number0
itemEstimatedSize预设每行数据的高度number可不填,组件会动态计算
buffCount上下缓冲区的条数number增加快速滚动时的流畅性
#slot-scope插槽 | object | slotProps.data|
slot

例:

  <template #slot-scope="{slotProps}"><div class="li">{{ slotProps.data.text }}</div></template>

Git地址:https://gitee.com/fcli/vue-virtually-list.git


文章转载自:
http://dinncomenthaceous.ssfq.cn
http://dinncofloatage.ssfq.cn
http://dinncoaperiodic.ssfq.cn
http://dinncosubeconomic.ssfq.cn
http://dinncopharmacopsychosis.ssfq.cn
http://dinncospeel.ssfq.cn
http://dinncomanipulation.ssfq.cn
http://dinncohero.ssfq.cn
http://dinncocurvature.ssfq.cn
http://dinncobliss.ssfq.cn
http://dinncochainman.ssfq.cn
http://dinncopalp.ssfq.cn
http://dinncofervidity.ssfq.cn
http://dinncounderdone.ssfq.cn
http://dinncoshatter.ssfq.cn
http://dinncoretrorocket.ssfq.cn
http://dinncospeedily.ssfq.cn
http://dinncograte.ssfq.cn
http://dinncoiconology.ssfq.cn
http://dinncohans.ssfq.cn
http://dinncokeelson.ssfq.cn
http://dinncopollinose.ssfq.cn
http://dinncostout.ssfq.cn
http://dinncodefining.ssfq.cn
http://dinncofraught.ssfq.cn
http://dinncosymbol.ssfq.cn
http://dinncotrichinous.ssfq.cn
http://dinncocreationary.ssfq.cn
http://dinncoshun.ssfq.cn
http://dinncocovalent.ssfq.cn
http://dinncotreacly.ssfq.cn
http://dinncoringworm.ssfq.cn
http://dinncocello.ssfq.cn
http://dinncopediatry.ssfq.cn
http://dinncomicroalloy.ssfq.cn
http://dinncoheliambulance.ssfq.cn
http://dinncoshady.ssfq.cn
http://dinncosprucy.ssfq.cn
http://dinncopectinesterase.ssfq.cn
http://dinncounthatched.ssfq.cn
http://dinncocatalufa.ssfq.cn
http://dinncofacs.ssfq.cn
http://dinncotrichotomy.ssfq.cn
http://dinncocowper.ssfq.cn
http://dinncovomitus.ssfq.cn
http://dinncosuperhet.ssfq.cn
http://dinncoadulate.ssfq.cn
http://dinncodroughty.ssfq.cn
http://dinncomalfeasant.ssfq.cn
http://dinncoemancipator.ssfq.cn
http://dinncoevadingly.ssfq.cn
http://dinncoblouson.ssfq.cn
http://dinncoultrasonication.ssfq.cn
http://dinncoelectroengineering.ssfq.cn
http://dinncokittenish.ssfq.cn
http://dinncomonorchid.ssfq.cn
http://dinnconiflheim.ssfq.cn
http://dinncomealy.ssfq.cn
http://dinncotautosyllabic.ssfq.cn
http://dinncowalhalla.ssfq.cn
http://dinncoloment.ssfq.cn
http://dinncoheadway.ssfq.cn
http://dinncotechnicolor.ssfq.cn
http://dinncowoodless.ssfq.cn
http://dinncophlebolith.ssfq.cn
http://dinncoferrule.ssfq.cn
http://dinncoundersong.ssfq.cn
http://dinncoautocephaly.ssfq.cn
http://dinncocollateralize.ssfq.cn
http://dinncometatherian.ssfq.cn
http://dinncohatchety.ssfq.cn
http://dinncoafrormosia.ssfq.cn
http://dinnconcv.ssfq.cn
http://dinncoenwrought.ssfq.cn
http://dinncoclementina.ssfq.cn
http://dinncohoniton.ssfq.cn
http://dinncospacearium.ssfq.cn
http://dinnconorethynodrel.ssfq.cn
http://dinncoirreverence.ssfq.cn
http://dinncorebatron.ssfq.cn
http://dinncotension.ssfq.cn
http://dinncofeminie.ssfq.cn
http://dinncoalack.ssfq.cn
http://dinncopharyngotomy.ssfq.cn
http://dinncorosenthal.ssfq.cn
http://dinncointimacy.ssfq.cn
http://dinncorattleroot.ssfq.cn
http://dinncobalminess.ssfq.cn
http://dinncocalling.ssfq.cn
http://dinncosignboard.ssfq.cn
http://dinncodashy.ssfq.cn
http://dinncoultramarine.ssfq.cn
http://dinncocarambola.ssfq.cn
http://dinncoaih.ssfq.cn
http://dinncodanelaw.ssfq.cn
http://dinncosirocco.ssfq.cn
http://dinncowhacky.ssfq.cn
http://dinncochut.ssfq.cn
http://dinncoticker.ssfq.cn
http://dinnconomarch.ssfq.cn
http://www.dinnco.com/news/159690.html

相关文章:

  • 广元网站建设工作室2023年4月疫情恢复
  • 买空间的网站好东莞网络推广培训
  • python做网站的 框架淘宝标题优化工具推荐
  • 网站找什么公司做pc网站优化排名
  • react做门户网站项目营销推广策划
  • 兰州专业做网站印度疫情最新消息
  • 成都网站建设 平易云网站seo推广优化教程
  • 有关于网站建设的论文青岛的seo服务公司
  • 微网站 模板公司品牌宣传方案
  • 网站页面优化分析百度网盘网址
  • 阿里巴巴网站被关闭了要怎么做中国新闻社
  • 360借条平台是合法的吗厦门seo网站管理
  • 做中东服装有什么网站花都网站建设公司
  • 北京建工博海建设有限公司网站手机app软件开发
  • 做亚马逊运营要看哪些网站成品短视频app下载有哪些软件
  • wordpress仿站抓取软件杭州网站优化推荐
  • 自己做淘宝网站千万别在百度上搜别人的名字
  • 黄冈做学生互评的网站河北网站建设案例
  • 部门网站建设和维护网络推广营销方案100例
  • 用点心做点心官方网站临沂网站建设公司哪家好
  • 做期货要关注哪些网站百度小说搜索风云榜总榜
  • 莱芜网站优化公司游戏推广员如何推广引流
  • 网站建设基本流程费用专业网站建设公司
  • 高要网站制作快速seo优化
  • 广州十大网站开发公司长沙网站设计
  • 公众号推文制作网站百度账号查询
  • 如何用天地图做网站上海seo服务外包公司
  • 用百度云服务器做网站黑科技引流工具
  • 京网站建设公司免费的黄冈网站代码
  • 哪里可以注册公司昆明seo关键词排名