列表找房(十)03-页面滚动效果控制——WindowScroller组件用法 & Scroll属性

页面滚动效果控制——WindowScroller组件用法 & Scroll属性

<WindowScroller>
  {({ height, isScrolling, scrollTop }) => {
     /*
       height:表示父窗口的高度
      isScrolling: 监听列表的滚动
      scrollTop:页面的滚动距离
     */
    return (
      <AutoSizer>
        {({ width }) => (
          <List
            autoHeight
            height={height}
            width={width}
            isScrolling={isScrolling}
            scrollTop={scrollTop}
            rowCount={this.state.count}
            rowHeight={140}
            rowRenderer={this.renderHouseItem}
          />
        )}
      </AutoSizer>
    )
  }}
</WindowScroller>

动态加载更多数据

  • 目前只能加载前20条数据,使用InfiniteLoader让List可以在滑动时加载更多数据

    • 基本使用

      • 导入对应组件

        import { InfiniteLoader, List } from 'react-virtualized';
        
      • 基本使用

        <InfiniteLoader
          isRowLoaded={isRowLoaded}
          loadMoreRows={loadMoreRows}
          rowCount={remoteRowCount}
        >
          {({ onRowsRendered, registerChild }) => (
            <List
              height={200}
              onRowsRendered={onRowsRendered}
              ref={registerChild}
              rowCount={remoteRowCount}
              rowHeight={20}
              rowRenderer={rowRenderer}
              width={300}
            />
          )}
        </InfiniteLoader>
        
  • 项目应用

isRowLoaded = ({ index }) => {
  return !!this.state.list[index]
}
loadMoreRows = ({ startIndex, stopIndex }) => {
  console.log(startIndex, stopIndex)
  // 加载更多数据
  return new Promise(async (resolve, reject) => {
    let res = await axios.get('houses', {
      params: {
        ...this.state.conditions,
        cityId: this.state.city.value,
        start: startIndex,
        end: stopIndex
      }
    })
    this.setState({
      total: res.body.count,
      // 获取到新的数据后进行累加操作
      listData: [...this.state.listData, ...res.body.list]
    }, () => {
      // 此次加载数据已经完成
      resolve()
    })
  })
}
<InfiniteLoader
  isRowLoaded={this.isRowLoaded}
  loadMoreRows={this.loadMoreRows}
  rowCount={this.state.count}>
{({ onRowsRendered, registerChild }) => (
  <WindowScroller>
    {({ height, isScrolling, scrollTop }) => {
      return (
        <AutoSizer>
          {({ width }) => (
            <List
              autoHeight
              height={height}
              width={width}
              isScrolling={isScrolling}
              scrollTop={scrollTop}
              rowCount={this.state.count}
              rowHeight={140}
              rowRenderer={this.renderHouseItem}
              onRowsRendered={onRowsRendered}
              ref={registerChild}
            />
          )}
        </AutoSizer>
      )
    }}
  </WindowScroller>
)}
</InfiniteLoader>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来一步步实现这个功能。 首先,我们需要安装 Element UI,可以通过 npm 安装: ``` npm install element-ui --save ``` 然后在 main.js 中引入并使用: ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 接下来,我们来实现时间线组件。可以在组件中使用 el-timeline 和 el-timeline-item 组件,代码如下: ```html <template> <el-timeline> <el-timeline-item v-for="(item,index) in list" :key="index" :timestamp="item.time">{{ item.content }}</el-timeline-item> </el-timeline> </template> ``` 其中,list 是传入组件的数据,包含每个时间点的内容和时间信息。 接着,我们来实现自动滚动功能。可以通过监听 el-timeline 的 scroll 事件,将 el-timeline 的 scrollTop 设置为最大值,让其自动滚动到底部。代码如下: ```html <template> <el-timeline ref="timeline" @scroll="handleScroll"> <el-timeline-item v-for="(item,index) in list" :key="index" :timestamp="item.time">{{ item.content }}</el-timeline-item> </el-timeline> </template> <script> export default { methods: { handleScroll() { const timelineEl = this.$refs.timeline.$el timelineEl.scrollTop = timelineEl.scrollHeight } } } </script> ``` 然后,我们来实现无限滚动和动态加载功能。可以使用 v-infinite-scroll 插件,通过监听 el-timeline 的滚动事件,在滚动到底部时触发加载更多数据的方法。代码如下: ```html <template> <el-timeline ref="timeline" @scroll="handleScroll" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading" infinite-scroll-distance="10"> <el-timeline-item v-for="(item,index) in list" :key="index" :timestamp="item.time">{{ item.content }}</el-timeline-item> <div v-if="loading">正在加载...</div> </el-timeline> </template> <script> import { InfiniteScroll } from 'element-ui' export default { directives: { InfiniteScroll }, data() { return { list: [], // 数据列表 loading: false // 是否正在加载 } }, methods: { // 加载数据 loadMore() { if (this.loading) { return } this.loading = true // 模拟异步加载数据 setTimeout(() => { const newData = [{ time: '2021-08-01', content: '新的内容' }] this.list = this.list.concat(newData) this.loading = false }, 1000) }, // 滚动事件 handleScroll() { const timelineEl = this.$refs.timeline.$el if (timelineEl.scrollTop === 0) { this.loadMore() } } } } </script> ``` 在上面的代码中,我们使用了 element-ui 中的 InfiniteScroll 指令来实现无限滚动功能。同时,我们在 el-timeline 中添加了 infinite-scroll-disabled 和 infinite-scroll-distance 属性,分别用于控制是否禁用无限滚动和触发加载更多数据的距离。 现在我们已经完成了 element-ui 的时间线组件 + 自动滚动 + v-infinite-scroll 无限滚动 + 动态加载功能的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值