使用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 相关文章推荐
JS应用之禁止抓屏、复制、打印
Feb 21 Javascript
JS生成随机字符串的多种方法
Jun 10 Javascript
聊一聊JS中this的指向问题
Jun 17 Javascript
对js中回调函数的一些看法
Aug 29 Javascript
JS复制对应id的内容到粘贴板(Ctrl+C效果)
Jan 23 Javascript
神级程序员JavaScript300行代码搞定汉字转拼音
May 20 Javascript
vue上传图片组件编写代码
Jul 26 Javascript
JS实现的简单表单验证功能示例
Oct 13 Javascript
vue自定义一个v-model的实现代码
Jun 21 Javascript
Node.js实现批量下载图片简单操作示例
Jan 18 Javascript
Node.js+Vue脚手架环境搭建的方法步骤
Mar 08 Javascript
详解Vue3 Teleport 的实践及原理
Dec 02 Vue.js
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
php创建多级目录代码
2008/06/05 PHP
Laravel框架基于中间件实现禁止未登录用户访问页面功能示例
2019/01/17 PHP
stripos函数知识点实例分享
2019/02/11 PHP
JavaScript与C# Windows应用程序交互方法
2007/06/29 Javascript
javascript 匿名函数的理解(透彻版)
2010/01/28 Javascript
js有序数组的连接问题
2013/10/01 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
jquery实现移动端点击图片查看大图特效
2020/09/11 Javascript
基于jQuery实现select下拉选择可输入附源码下载
2016/02/03 Javascript
jQuery实现根据滚动条位置加载相应内容功能
2016/07/18 Javascript
jquery根据td给相同tr下其他td赋值的实现方法
2016/10/05 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
微信小程序中form 表单提交和取值实例详解
2017/04/20 Javascript
SVG实现时钟效果
2018/07/17 Javascript
layui table设置前台过滤转义等方法
2018/08/17 Javascript
详解jQuery中的easyui
2018/09/02 jQuery
vue-router的HTML5 History 模式设置
2018/09/08 Javascript
利用JS动态生成隔行换色HTML表格的两种方法
2018/10/09 Javascript
Django框架教程之正则表达式URL误区详解
2018/01/28 Python
python中的数据结构比较
2019/05/13 Python
python射线法判断检测点是否位于区域外接矩形内
2019/06/28 Python
python飞机大战pygame碰撞检测实现方法分析
2019/12/17 Python
Python开发之基于模板匹配的信用卡数字识别功能
2020/01/13 Python
35款精致的 CSS3 和 HTML5 网页模板 推荐
2012/08/03 HTML / CSS
美国户外运动商店:Sun & Ski
2018/08/23 全球购物
一加手机美国官方网站:OnePlus美国
2019/09/19 全球购物
六个一活动实施方案
2014/03/21 职场文书
企业厂务公开实施方案
2014/03/26 职场文书
《小猪家的桃花树》教学反思
2014/04/11 职场文书
广播体操口号
2014/06/18 职场文书
行政专员岗位职责范本
2014/08/26 职场文书
2015年医务人员医德医风自我评价
2015/03/03 职场文书
中学综治宣传月活动总结
2015/05/07 职场文书
新闻通讯稿模板
2015/07/22 职场文书
高三生物教学反思
2016/02/22 职场文书
关于感恩的歌曲整理(8首)
2019/08/14 职场文书