使用react-virtualized实现图片动态高度长列表的问题


Posted in Javascript onMay 28, 2021

虚拟列表是一种根据滚动容器元素的可视区域来渲染长列表数据中某一个部分数据的技术。虚拟列表是对长列表场景一种常见的优化,毕竟很少有人在列表中渲染上百个子元素,只需要在滚动条横向或纵向滚动时将可视区域内的元素渲染出即可。

开发中遇到的问题

1.长列表中的图片要保持原图片相同的比例,那纵向滚动在宽度不变的情况下,每张图片的高度就是动态的,当该列表项高度发生了变化,会影响该列表项及其之后所有列表项的位置信息。

2.图片width,height必须在图片加载完成后才能获得.

解决方案

我们使用react-virtualized中list组件,官方给出的例子

import React from 'react';
import ReactDOM from 'react-dom';
import {List} from 'react-virtualized';

// List data as an array of strings
const list = [
  'Brian Vaughn',
  // And so on...
];

function rowRenderer({
  key, // Unique key within array of rows
  index, // Index of row within collection
  isScrolling, // The List is currently being scrolled
  isVisible, // This row is visible within the List (eg it is not an overscanned row)
  style, // Style object to be applied to row (to position it)
}) {
  return (
    <div key={key} style={style}>
      {list[index]}
    </div>
  );
}

// Render your list
ReactDOM.render(
  <List
    width={300}
    height={300}
    rowCount={list.length}
    rowHeight={20}
    rowRenderer={rowRenderer}
  />,
  document.getElementById('example'),
);

使用react-virtualized实现图片动态高度长列表的问题

其中rowHeight是每一行的高度,可以传入固定高度也可以传入function。每次子元素高度改变需要调用recomputeRowHeights方法,指定索引后重新计算行高度和偏移量。

具体实现

const ImgHeightComponent = ({ imgUrl, onHeightReady, height, width }) => {
  const [style, setStyle] = useState({
    height,
    width,
    display: 'block',
  })
  const getImgWithAndHeight = (url) => {
    return new Promise((resolve, reject) => {
      var img = new Image()
      // 改变图片的src
      img.src = url
      let set = null
      const onload = () => {
        if (img.width || img.height) {
          //图片加载完成
          clearInterval(set)
          resolve({ width: img.width, height: img.height })
        }
      }
      set = setInterval(onload, 40)
    })
  }

  useEffect(() => {
    getImgWithAndHeight(imgUrl).then((size) => {
      const currentHeight = size.height * (width / size.width)
      setStyle({
        height: currentHeight,
        width: width,
        display: 'block',
      })
      onHeightReady(currentHeight)
    })
  }, [])
  return <img src={imgUrl} alt=''  style={style} />
}

先写一个获取图片高度的组件,通过定时循环检测获取并计算出高度传给父组件。

import React, { useState, useEffect, useRef } from 'react'
import styles from './index.scss'
import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer'
import { List  } from 'react-virtualized/dist/commonjs/List'

export default class DocumentStudy extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      list: [], 
      heights: [],
      autoWidth:900,
      autoHeight: 300
    }
  }

  handleHeightReady = (height, index) => {
    this.setState(
      (state) => {
        const flag = state.heights.some((item) => item.index === index)
        if (!flag) {
          return {
            heights: [
              ...state.heights,
              {
                index,
                height,
              },
            ],
          }
        }
        return {
          heights: state.heights,
        }
      },
      () => {
        this.listRef.recomputeRowHeights(index)
      },
    )
  }

  getRowHeight = ({ index }) => {
    const row = this.state.heights.find((item) => item.index === index)
    return row ? row.height : this.state.autoHeight
  }

  renderItem = ({ index, key, style }) => {
    const { list, autoWidth, autoHeight } = this.state
    if (this.state.heights.find((item) => item.index === index)) {
      return (
        <div key={key} style={style}>
          <img src={list[index].imgUrl}  alt='' style={{width: '100%'}}/>
        </div>
      )
    }

    return (
      <div key={key} style={style}>
        <ImgHeightComponent
          imgUrl={list[index].imgUrl}
          width={autoWidth}
          height={autoHeight}
          onHeightReady={(height) => {
            this.handleHeightReady(height, index)
          }}
        />
      </div>
    )
  }

  render() {
    const { list } = this.state
    return (
      <>
        <div style={{ height: 1000 }}>
          <AutoSizer>
            {({ width, height }) => (
              <List
                ref={(ref) => (this.listRef = ref)}
                width={width}
                height={height}
                overscanRowCount={10}
                rowCount={list.length}
                rowRenderer={this.renderItem}
                rowHeight={this.getRowHeight}
              />
            )}
          </AutoSizer>
        </div>
      </>
    )
  }
}

父组件通过handleHeightReady方法收集所有图片的高度,并在每一次高度改变调用List组件的recomputeRowHeights方法通知组件重新计算高度和偏移。到这里基本已经解决遇到的问题。

实际效果

使用react-virtualized实现图片动态高度长列表的问题

小结

目前只是使用react-virtualized来完成图片长列表实现,具体react-virtualized内部实现还需要进一步研究。

以上就是用react-virtualized实现图片动态高度长列表的详细内容,更多关于react virtualized长列表的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
JavaScript的变量作用域深入理解
Oct 25 Javascript
javascript实现可改变滚动方向的无缝滚动实例
Jun 17 Javascript
js数组与字符串的相互转换方法
Jul 09 Javascript
Bootstrap入门书籍之(五)导航条、分页导航
Feb 17 Javascript
AngularJS报错$apply already in progress的解决方法分析
Jan 30 Javascript
简单的网页广告特效实例
Aug 19 Javascript
解决vue+webpack打包路径的问题
Mar 06 Javascript
在vue项目中,使用axios跨域处理
Mar 07 Javascript
通过一次报错详细谈谈Point事件
May 17 Javascript
vue.js使用v-model指令实现的数据双向绑定功能示例
May 22 Javascript
JavaScript工具库MyTools详解
Jan 01 Javascript
探索node之事件循环的实现
Oct 30 Javascript
element多个表单校验的实现
May 27 #Javascript
springboot+VUE实现登录注册
May 27 #Vue.js
vue+springboot实现登录验证码
vue+spring boot实现校验码功能
May 27 #Vue.js
vue-cropper组件实现图片切割上传
May 27 #Vue.js
vue-cropper插件实现图片截取上传组件封装
May 27 #Vue.js
HTML+VUE分页实现炫酷物联网大屏功能
You might like
手把手教你使用DedeCms V3的在线采集图文教程
2007/04/03 PHP
php URL编码解码函数代码
2009/03/10 PHP
PHP 关于访问控制的和运算符优先级介绍
2013/07/08 PHP
PHP对象克隆clone用法示例
2016/09/28 PHP
PHP利用Mysql锁解决高并发的方法
2018/09/04 PHP
11款基于Javascript的文件管理器
2009/10/25 Javascript
javascript与jquery中跳出循环的区别总结
2013/11/04 Javascript
js 删除数组的几种方法小结
2014/02/21 Javascript
jquery+json实现数据二级联动的方法
2015/11/28 Javascript
javascript执行环境及作用域详解
2016/05/05 Javascript
Js获取当前日期时间及格式化代码
2016/09/17 Javascript
微信小程序实现多选功能
2018/11/04 Javascript
JavaScript实现简单贪吃蛇效果
2020/03/09 Javascript
node koa2 ssr项目搭建的方法步骤
2020/12/11 Javascript
在python的WEB框架Flask中使用多个配置文件的解决方法
2014/04/18 Python
Tornado 多进程实现分析详解
2018/01/12 Python
python基础教程项目五之虚拟茶话会
2018/04/02 Python
Redis使用watch完成秒杀抢购功能的代码
2018/05/07 Python
python 利用栈和队列模拟递归的过程
2018/05/29 Python
解决在pycharm中显示额外的 figure 窗口问题
2019/01/15 Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
2019/03/27 Python
对Python获取屏幕截图的4种方法详解
2019/08/27 Python
Pyqt5自适应布局实例
2019/12/13 Python
关于css兼容性问题及一些常见问题汇总
2016/05/03 HTML / CSS
轻松掌握CSS3中的字体大小单位rem的使用方法
2016/05/24 HTML / CSS
美国瑜伽服装和装备购物网站:Mukha Yoga
2019/02/22 全球购物
神话般的珠宝:Ross-Simons
2020/07/13 全球购物
会计大学生职业生涯规划书范文
2014/01/13 职场文书
优秀广告词大全
2014/03/19 职场文书
教师批评与自我批评(群众路线)
2014/10/15 职场文书
2016情人节宣传语
2015/07/14 职场文书
小学班长竞选稿
2015/11/20 职场文书
人生感悟经典句子
2019/08/20 职场文书
redis配置文件中常用配置详解
2021/04/14 Redis
Golang 实现超大文件读取的两种方法
2021/04/27 Golang
python游戏开发之pygame实现接球小游戏
2022/04/22 Python