当前位置:网站首页>The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
2022-07-24 18:10:00 【Hard working classmate Zhu】
One 、iScroll.js
iScroll It's a high performance , Less resources , Without relying on , Multiple platforms javascript Scrolling plug-ins ,PC End 、 Mobile terminals can be used , stay Vue、Nuxt And other engineering projects can also be used , Application scenarios are widely used . Please refer to file .
Two 、 Problems encountered
1、 Scene description
I was in Nuxt In the project , It encapsulates a drop-down box component for keyword remote search , After entering the keywords , Go to the background to search for relevant data , Only 5 Data , You need to scroll down to see other data , Then in order to avoid too much data , Limit the loading of ten pieces of data at a time , Load more data by clicking load more at the end of the list .
2、 Problem description and solution
The first pit :
In the introduction of iScroll.js after , First of all, we need to declare a global IScroll object , stay Nuxt You need to use Window object , To declare IScroll Object is mounted to the global :
// Statement IScroll object And mount it globally
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
In the beginning, I was in Beijing Life cycle mounted Stage life and mount the object , Found after running ,iScroll It doesn't work , The drop-down list cannot be scrolled , But through developer tools , Find out iScroll The relevant properties of have appeared in dom In the . After a series of checks , The discovery is due to , The data rendered by the drop-down list is obtained from the background , When we're in mounted Stage mount IScroll When the object , List data has not been obtained from background and rendered , The page doesn't exist yet dom Elements , therefore iScroll Cannot get the corresponding element , Unable to tell the true length of the list , IScroll Objects will not work , The scroll bar will not appear .
Solution :
adopt this.$nextTick(), When the page is rendered , Then go to declare and mount IScroll object :
async getSchoolList () {
// Show only... At a time 10 Article related data
const params = {
pageIndex: this.pageIndex,
pageSize: 10
}
// Go to the background to get data
const res = await this.$axios.post('/api/', params)
// Wait until the page is rendered , Then declare and mount
this.$nextTick(() => {
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
})
},
The second pit :
After stepping on the pit in front , Very smoothly, I fell into the second pit , As I said before , In order to avoid too much data , The logarithm is paged , Only load... At a time 10 Data , And then there's the problem , First load 10 After data ,iScroll There is no problem with the generated scroll bar . But load again 10 When the data , The problem arises , Because the list at this time has 20 Bar data , List length becomes longer , The scroll bar should be smaller . The scroll bar variable is really small , But for the first time 10 The scroll bar generated when the data is displayed does not disappear , It's the heel 20 The scrollbars of data overlap ,30 strip 、40 The same is true of article , It's hard .
Solution :
This already belongs to iScroll.js Its own defects , It is unrealistic to modify its underlying code , So I thought of a trick , Every time you re render , Hide the front roll strip :
async getSchoolList () {
// Show only... At a time 10 Article related data
const params = {
pageIndex: this.pageIndex,
pageSize: 10
}
const res = await this.$axios.post('/api/', params)
// When the data increases iscroll The original scroll bar still exists So hide all the previous scrollbars
const n = document.querySelectorAll('.iScrollIndicator')
n.forEach((item) => {
item.style.display = 'none'
})
// Wait until the page is rendered , Then declare and mount Generate a new scroll bar
this.$nextTick(() => {
window.myScroll = new window.IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true,
startY: scrollY
})
})
},
边栏推荐
- Quickly complete the unit test junit4 setting of intelij idea
- How to quickly upload files to Google Lab
- Cookies and session "suggestions collection"
- 0621~ES&Lucene
- Awk from entry to earth (17) awk multiline writing
- 0629 ~ SaaS platform design ~ global exception handling
- Get the data of Tongcheng (elong) Hotel
- 手写博客平台~第二天
- sklearn 的模型保存与加载使用
- 6126. 设计食物评分系统
猜你喜欢

0630~ professional quality course

C language to achieve a static version of the address book

PXE efficient batch network installation

如何用WebGPU流畅渲染百万级2D物体?

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

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

继承与派生

Use prometheus+grafana to monitor MySQL performance indicators

How does win11 enhance the microphone? Win11 enhanced microphone settings

使用Prometheus+Grafana监控MySQL性能指标
随机推荐
Inheritance and Derive
Mac database management software Navicat premium essentials mac
Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)
redis集群的三种方式
Flink operation Hudi data table
What are the pitfalls from single architecture to distributed architecture?
0629~SaaS平台设计~全局异常处理
数组扁平化.flat(Infinity)
Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
0625~<config>-<bus>
获取1688app上原数据 API
Handwritten blog platform ~ the next day
阿里巴巴1688按关键字搜索商品 API 使用展示
[leetcode] 30. Concatenate substrings of all words
字符串常用方法(2)
Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
文件上传漏洞——.user.ini与.htaccess
[OBS] cooperation between video and audio coding and RTMP transmission
Blackmagic Fusion Studio 18