当前位置:网站首页>下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
2022-07-24 18:09:00 【努力的小朱同学】
一、iScroll.js
iScroll是一个高性能,资源占用少,无依赖,多平台的javascript滚动插件,PC端、移动端都可以使用,在Vue、Nuxt等工程化项目中也可以使用,应用场景使用广泛。具体使用方法请查阅文档。
二、遇到的问题
1、场景描述
我是在 Nuxt 项目中,封装了一个关键词远程搜索的下拉框组件,输入关键词后,去后台搜索相关数据,下拉框内只能展示5条数据,需要向下滚动来查看其他数据,然后为避免数据量过大,限制每次加载十条数据,通过点击列表最后的加载更多项来加载更多数据。
2、 问题描述以及解决方案
第一个坑:
在引入iScroll.js之后,首先要声明一个全局的IScroll对象,在 Nuxt 则需要借助 Window 对象,来将声明的IScroll对象挂载到全局:
// 声明 IScroll 对象 并挂载到全局
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
一开始我是在 生命周期的mounted阶段生命并挂载的该对象,运行之后发现,iScroll 不起作用,下拉列表无法滚动,但是通过开发者工具,发现iScroll的相关属性已经出现在dom里了。在经过一系列的排查后,发现是因为,下拉列表渲染的数据是从后台获取的,当我们在 mounted 阶段挂载 IScroll 对象的时候,列表数据还并没有从后台获取到并渲染,页面还并不存在该dom元素,所以 iScroll 获取不到对应的元素,无法分辨出列表的真实长度 , IScroll 对象也就无法起作用,滚动条也不会出现。
解决方案:
通过this.$nextTick(),当页面渲染完成之后,再去声明和挂载 IScroll 对象:
async getSchoolList () {
// 每次只显示10条相关数据
const params = {
pageIndex: this.pageIndex,
pageSize: 10
}
// 去后台获取数据
const res = await this.$axios.post('/api/', params)
// 待页面渲染完成后,再声明和挂载
this.$nextTick(() => {
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
})
},
第二个坑:
踩过前面的坑之后,非常顺利的我又掉进的第二个坑,前面说过了,为了避免数据量过大,对数做了分页处理,每次只加载10条数据,然后问题就出现了,第一次加载10条数据后,iScroll 生成的滚动条没有任何问题。但是再次加载10条数据时,问题出现了,因为此时的列表有20条数据了,列表长度变长,滚动条应该变小。滚动条确实变量小了,但是第一次10条数据时生成的滚动条并没有消失,而是跟20条数据的滚动条出现了重叠,30条、40条也是如此,这就很难受。
解决方案:
这已经是属于 iScroll.js 本身的缺陷了,修改其底层代码不现实,于是我想了一个取巧的法子,每次重新渲染的时候,都将前面的滚送条给隐藏掉:
async getSchoolList () {
// 每次只显示10条相关数据
const params = {
pageIndex: this.pageIndex,
pageSize: 10
}
const res = await this.$axios.post('/api/', params)
// 当数据增加之后 iscroll 原来的滚动条还存在 所以要将之前的所有滚动条隐藏掉
const n = document.querySelectorAll('.iScrollIndicator')
n.forEach((item) => {
item.style.display = 'none'
})
// 待页面渲染完成后,再声明和挂载 生成一个新的滚动条
this.$nextTick(() => {
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
})
},
边栏推荐
猜你喜欢

深入解析著名的阿里云Log4j 漏洞

Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research

The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience

ROC and AUC details of the recommended system

【obs】依赖库: x264 vs 构建

手写博客平台~第二天

After separation, the impression notes are still difficult to live, but there are many coquettish operations
![[network security] analysis vulnerability of website Middleware](/img/3a/9c034c17d65348aa7c35a3dac2039c.png)
[network security] analysis vulnerability of website Middleware

邻接表的定义和存储以及有向图无向图的邻接存储

Emerging potential of interactive virtual reality technology in drug discovery
随机推荐
移动端实现0.5px的实用方案
Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
1688/阿里巴巴按关键字搜索新品数据 API 使用说明
阿里巴巴/166获得店铺的所有商品 API使用说明
How to quickly upload files to Google Lab
Alibaba /1688 API instructions for searching products by map (pailitao)
Codeforces Round #794 (Div. 2)(A.B.C)
Wu Enda writes: how to establish projects to adapt to AI career
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷!
Guess JWT keyword
0613 ~ self study
Learn redisson from scratch ------- topics (subscription and distribution)
Still reading logs on the command line? Use kibana, visual log analysis yyds!
Growth of operation and maintenance Xiaobai - week 8 of Architecture
C language programming training topics: K characters in left-handed string, little Lele and Euclidean, printing arrow pattern, civil servant interview, poplar matrix
new也可以创建对象,为什么需要工厂模式?
Just one dependency to give swagger a new skin, which is simple and cool!
mac数据库管理软件Navicat Premium Essentials Mac
Use of jumpserver
redis集群的三种方式