Skip to content
On this page

滚动加载

瀑布流滚动加载,用于展示长列表,当列表即将滚动到底部时,会触发事件并加载更多列表项。

基础用法

1

2

3

4

5

加载中...


示例代码

vue
<template>
  <button @click="scrollRef.setScroll(100)">100</button>
  <button @click="scrollRef.setScroll(200)">200</button>
  <YikScroll
    ref="scrollRef"
    :loading="loading"
    @onBottom="handleBottom"
    @onTop="handleTop"
    @onWatch="handleWatch"
    style="height: 100px; border: 1px solid rgb(14, 190, 234)"
  >
    <p class="p" v-for="item in 5">
      {{ item }}
    </p>
  </YikScroll>
  <br />
  <div ref="content" class="console">
    <p v-for="(item, index) in list" :key="index">{{ item }}</p>
  </div>
</template>
<script setup>
import { ref, nextTick, reactive, toRefs } from "vue";
const scrollRef = ref(null);
const content = ref(null);
const state = reactive({
  list: [],
  loading: true,
  scroll: 10,
});
const { list, loading, scroll } = toRefs(state);
const handleBottom = () => {
  state.list.push("bottom");
  setTimeout(() => {
    state.loading = false;
  }, 3000);
  setTop();
};
const handleTop = () => {
  state.list.push("top");
  setTop();
};
const handleWatch = (_data) => {
  state.list.push(JSON.stringify(_data));
  setTop();
};
const setTop = () => {
  nextTick(() => {
    content.value.scrollTop = content.value.scrollHeight;
  });
};
</script>
<style lang="less" scoped>
.p {
  height: 50px;
  margin-bottom: 10px;
}
.console {
  height: 100px;
  overflow: auto;
  background-color: #000;
  color: #00db12;
}
button {
  padding: 5px 10px;
  background-color: #1a89fa;
  font-size: 14px;
  color: #fff;
  border-radius: 5px;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>

属性

属性名说明类型默认值
loading是否处于加载状态Booleantrue

事件

事件名说明回调参数
onBottom滚动到底部触发-
onTop滚动到顶部触发-
onWatch滚动时触发示例中有返回参数

插槽

名称说明
default列表内容
loading自定义底部加载中提示
finished自定义加载完成后的提示文案

方法

名称说明参数返回值
setScroll设置滚动值(scrollValue, autoLoadScroll = false):(滚动条值,是否自动开启请求)-
getScroll获取滚动值-Number