如何优化element穿梭框性能问题,避免渲染节点多卡顿
Admin 2022-07-01 群英技术资讯 1487 次浏览
这篇文章给大家分享的是如何优化element穿梭框性能问题,避免渲染节点多卡顿。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。穿梭框处理大数据量时,由于渲染的 DOM 节点过多,造成页面卡顿的问题。
在尽量不改变组件原有逻辑的前提下,进行优化。
懒加载 - InfiniteScroll 组件
先从 packages/transfer 中将原组件拷出(或者改源码重新打包维护私有库使用)
将
v-infinite-scroll="pageDown" :infinite-scroll-immediate="false"
添加到
<el-checkbox-group
v-show="!hasNoMatch && data.length > 0"
v-model="checked"
:size="size"
:class="{ 'is-filterable': filterable }"
class="el-transfer-panel__list"
v-infinite-scroll="pageDown"
:infinite-scroll-immediate="false"
>
<el-checkbox
class="el-transfer-panel__item"
:label="item[keyProp]"
:disabled="item[disabledProp]"
:key="item[keyProp]"
v-for="item in filteredData">
<option-content :option="item"></option-content>
</el-checkbox>
</el-checkbox-group>
在data中定义pageSize: 20 用来表示每页数据个数showData: [] 仅用来展示使用,替换上述代码中实际需要操作的数据 filteredData
v-for="item in showData">
同时在watch中相应的处理
data (data) {
const checked = [];
this.showData = data.slice(0, this.pageSize);
const filteredDataKeys = this.filteredData.map(
(item) => item[this.keyProp]
);
this.checked.forEach((item) => {
if (filteredDataKeys.indexOf(item) > -1) {
checked.push(item);
}
});
this.checkChangeByUser = false;
this.checked = checked;
},
filteredData (filteredData) {
this.showData = filteredData.slice(0, this.pageSize);
}
初始化展示数量随意这里取 20。
最后添加滚动到底部时调用的方法
pageDown () {
const l = this.showData.length;
const totalLength = this.filteredData.length
l < totalLength &&
(this.showData = this.filteredData.slice(0, l + this.pageSize > totalLength ?
totalLength : l + this.pageSize));
},
往下滚动的时候 展示的数据长度增加 20(数量随意), 超出时展示最大长度。
由此基本解决大数据量操作卡顿的问题。由于展示和逻辑层分开,组件的所有操作逻辑无须修改,最小程度减少差异。
手动滚动到列表末端,再进行搜索操作依然存在卡顿问题。
在滚动过程中,实际上顶端的数据依旧无法看见,该数据不展示,对用户体验也没有影响,
所以只需展示当前页的 20 条数据。
我们为el-checkbox-group添加一个 ref=scrollContainer 以便操作滚动条,
在data中定义当前页数 curIndex: 1
并对 pageDown 方法进行修改
pageDown () {
const totalLength = this.filteredData.length
if((this.curIndex*this.pageSize) < totalLength){
this.curIndex ++
const targetLength = this.curIndex * this.pageSize
const endPoint = targetLength > totalLength ? totalLength : targetLength
const startPoint = endPoint - this.pageSize > 0 ? endPoint - this.pageSize : 0
this.showData = this.filteredData.slice(startPoint, endPoint);
this.$refs.scrollContainer.$el.scrollTop = "1px" //滚动条到最上端,衔接下一页,为 0 可能会触发边界问题
}
}
为此我们还需要添加向上翻页的方法
InfiniteScroll 指令 只提供向下滚动,我们可以拓展该指令亦可自行添加上滑滚动监听
mounted(){
this.$refs.scrollContainer.$el.addEventListener('scroll', this.pageUp)
},
beforeDestroy(){
this.$refs.scrollContainer.$el.removeEventListener('scroll', this.pageUp)
},
注册pageUp 方法
pageUp(e){
if(e.target.scrollTop ===0 && this.curIndex>1){
this.curIndex --
const endPoint = this.curIndex * this.pageSize
const startPoint = (this.curIndex-1)* this.pageSize
this.showData = this.filteredData.slice(startPoint, endPoint);
const el = this.$refs.scrollContainer.$el
el.scrollTop = el.scrollHeight - el.clientHeight - 1 // 滚动到最底部,衔接上一页, -1 防止边界问题。
}
},
当进行数据操作的时候,页面内容变化,滚动条也会随之变化,为防止不能预知的翻页,数据改变时,重置滚动条和当前页码。
initScroll(){
this.curIndex = 1
this.$refs.scrollContainer.$el.scrollTop = 0
},
同时地,在watch中相应时候执行 initScroll
data(){
...
this.initScroll()
...
},
filteredData (filteredData) {
...
this.initScroll()
}
至此大数据量的穿梭框,性能大为改善。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
js判断在哪个浏览器打开项目的方法,通过以下方法判断浏览器
这篇文章主要介绍了JavaScript时间复杂度和空间复杂度,时间复杂度和空间复杂度是衡量一个算法是否优秀的标准,通常我们比较两个算法时会用预先估算和事后统计,下文详细介绍,需要的朋友可以参考一下
js中的this关键字平时在开发中使用时倒是也能正常应用,但是对其使用和判断并不能信手拈来,所以下面这篇文章主要给大家介绍了关于js中this关键字的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
在React中,你可以创建不同的组件来封装各种你需要的行为。然后还可以根据应用的状态变化只渲染其中的一部分。React 中的条件渲染和JavaScript中的一致,使用JavaScript操作符if或条件运算符来创建表示当前状态的元素,然后让React根据它们来更新UI
从零开始在vue-cli4配置自适应vw布局的实现, 简介<br /> viewportWidth也是vw布局从配置上来说比rem布局简洁了很多,bu需要配置安装lib,也不需要增加一个rem.js文件<br /> 简称拎包使用<br /> 安装包<br /> <br /> <br /> npm install postcss-px-t
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008