May 07, 2021 Vant
Waterfall flow scroll loading, which is used to display long lists that trigger events and load more list items when the list is about to scroll to the bottom.
import Vue from 'vue';
import { List } from 'vant';
Vue.use(List);
The List component controls the loading state through two variables, loading and setting the load to true when the component scrolls to the bottom. A t this point, you can initiate asynchronous operations and update the data, after the data is updated, set the loading to false. If the data is fully loaded, set the finished directly to true.
<van-list
v-model="loading"
:finished="finished"
finished-text="没有更多了"
@load="onLoad"
>
<van-cell v-for="item in list" :key="item" :title="item" />
</van-list>
export default {
data() {
return {
list: [],
loading: false,
finished: false
};
},
methods: {
onLoad() {
// 异步更新数据
// setTimeout 仅做示例,真实场景中一般为 ajax 请求
setTimeout(() => {
for (let i = 0; i < 10; i++) {
this.list.push(this.list.length + 1);
}
// 加载状态结束
this.loading = false;
// 数据全部加载完成
if (this.list.length >= 40) {
this.finished = true;
}
}, 1000);
}
}
}
If the list data fails to load, setting the error to true displays an error prompt, and the load event is re-triggered when the user clicks the error prompt.
<van-list
v-model="loading"
:error.sync="error"
error-text="请求失败,点击重新加载"
@load="onLoad"
>
<van-cell v-for="item in list" :key="item" :title="item" />
</van-list>
export default {
data() {
return {
list: [],
error: false,
loading: false
};
},
methods: {
onLoad() {
fetchSomeThing().catch(() => {
this.error = true;
})
}
}
}
The List component can be used in conjunction with the PullRefresh component for a pull-down refresh
<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
<van-list
v-model="loading"
:finished="finished"
finished-text="没有更多了"
@load="onLoad"
>
<van-cell v-for="item in list" :key="item" :title="item" />
</van-list>
</van-pull-refresh>
export default {
data() {
return {
list: [],
loading: false,
finished: false,
refreshing: false
};
},
methods: {
onLoad() {
setTimeout(() => {
if (this.refreshing) {
this.list = [];
this.refreshing = false;
}
for (let i = 0; i < 10; i++) {
this.list.push(this.list.length + 1);
}
this.loading = false;
if (this.list.length >= 40) {
this.finished = true;
}
}, 1000);
},
onRefresh() {
// 清空列表数据
this.finished = false;
// 重新加载数据
// 将 loading 设置为 true,表示处于加载状态
this.loading = true;
this.onLoad();
}
}
}
Parameters | Description | Type | The default |
---|---|---|---|
v-model |
Whether it is loaded or not, the load event is not
load
the load process
|
boolean |
false
|
finished |
Whether the load is complete and the load event is no longer triggered
load
complete
|
boolean |
false
|
error |
Whether the load failed, the click error prompt can be re-clicked after the load failed
To
load
event, you must use the
sync
modifier
|
boolean |
false
|
offset |
The load event is triggered when the scroll bar is less than offset from
load
bottom
|
number | string |
300
|
loading-text | The prompt file during loading | string |
加载中...
|
finished-text | Prompt file after loading is complete | string | - |
error-text | Prompt file after failed load | string | - |
immediate-check | Whether to perform a rolling position check immediately upon initialization | boolean |
true
|
direction |
Scroll to trigger the direction of the load, with an optional
up
|
string |
down
|
The name of the event | Description | Callback parameters |
---|---|---|
load | Triggered when the scroll bar is less than offset from the bottom | - |
Ref allows you to get to the List instance and call the instance method, as detailed in the component instance method
The method name | Description | Parameters | Returns a value |
---|---|---|---|
check | Check the current scrolling position and, if scrolled to the bottom, the load event is triggered | - | - |
Name | Description |
---|---|
default | The contents of the list |
loading | Custom bottom load prompts |
finished | Custom prompt file after loading is complete |
error | Prompt for a file after a custom load failure |
List listens for scrolling events in the browser and calculates the location of the list, triggering a load event when the distance from the visual area at the bottom of the list is less than offset.
List initialization triggers a load event to load the first screen of data, a feature that can be turned off by the immediate-check property.
If a request loads a small number of data bars, causing the list content to not be able to cover the current screen, List continues to trigger the load event until the content is full or the data is fully loaded. So you need to adjust the number of data bars you get each time, and ideally the number of bars you get per request should be able to fill a screen height.
List has three states that help you use the List component correctly:
After each request is complete, you need to manually set the load to false, indicating that the load is over
If list content uses a float layout, you can add a van-clearfix class name to the container to clear the float, allowing List to correctly determine the location of the element
<van-list>
<div class="van-clearfix">
<div class="float-item" />
<div class="float-item" />
<div class="float-item" />
</div>
</van-list>
If you set the overflow-x: hidden style on the html and body tabs, the List causes the load to be triggered all the time.
html,
body {
overflow-x: hidden;
}
The problem is that when an element is set to overflow-x: hidden, the overflow-y of the element is set by the browser to auto instead of the default value, causing List to fail to correctly determine the scroll container. The workaround is to remove the style, or add a height: 100% style to the html and body labels.