zoukankan      html  css  js  c++  java
  • react 瀑布流

    pull.tsx

    import { View, Image, ScrollView } from '@tarojs/components'
    import Taro, { Component } from '@tarojs/taro'
    
    import './index.scss'
    
    type Props = {
    }
    
    type State = {
      list: Object[],
      leftList: Object[],
      rightList: Object[],
      leftHeight: Number,
      rightHeight: Number
    }
    
    let leftHeight = 0
    let rightHeight = 0
    
    class Pull extends Component<Props, State> {
      constructor(props) {
        super(props)
        this.state = {
          list: [{ CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/Tj7a2JfPzH_.jpg' },
          { CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/WQXrEJGXfX_.jpg' },
          { CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/ZbZpfpmGCk_.jpg' },
          { CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/DmeGWxFBGD_.png' },
          { CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/F7s3cRhFj8_.jpg' },
          { CoverWidth: '', CoverHeight: '', Cover: 'https://img0.tking.cn/mtl/default/img/jPCEEkTAf4_.jpg' }],
          leftList: [],
          rightList: [],
        }
      }
    
      componentDidMount() {
        const { list } = this.state;
    
        for (const item of list) {
          Taro.getImageInfo({ src: item.Cover }).then((res) => {
            item.CoverWidth = res.width + 'px'
            item.CoverHeight = res.height + 'px'
          })
        }
        console.log(list)
    
        this.isLeft()
      }
    
      async isLeft() {
        const { list, leftList, rightList } = this.state;
        for (const item of list) {
          leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度,来觉得添加到那边
          await this.getBoxHeight(leftList, rightList);
        }
      }
    
      getBoxHeight(leftList, rightList) { //获取左右两边高度
    
        let query = Taro.createSelectorQuery();
        return new Promise((resolve) => {
          this.setState({ leftList, rightList }, () => {
            setTimeout(() => {
              query.select('#left').boundingClientRect();
              query.select('#right').boundingClientRect();
              query.exec((res) => {
                leftHeight = res[0].height;
                rightHeight = res[1].height;
                resolve();
              });
            }, 50);
          });
        })
      }
    
      render() {
        const { leftList, rightList } = this.state;
    
        return (
          <ScrollView
            enableFlex={true}
            className='content'>
            <View className='left' id="left" ref="left">
              {leftList.map((item, index) => {
                return <View key={index}>
                  <Image lazyLoad mode="widthFix" className='pic' src={item.Cover}></Image>
                </View>
              })}
            </View>
            <View className='right' id="right">
              {rightList.map((item, index) => {
                return <View key={index}>
                  <Image lazyLoad mode="widthFix" className='pic' src={item.Cover}></Image>
                </View>
              })}
            </View>
            <View className="clear"></View>
          </ScrollView>
        )
      }
    }
    
    export default Pull
    

    pull.scss

    .content{
      // flex-direction: column;
    }
    
    .left{
       47%;
      float: left;
      margin:0 1% 0 2%;
    }
    
    .right{
       47%;
      float: right;
      margin:0 2% 0 1%;
    }
    
    .pic{
      border-radius: 10rpx;
       100%;
    }
    .clear{
      clear: both;
    }
    

      

  • 相关阅读:
    小程序开发日志-1、小程序自带的日志功能
    java判断List里面的值是否存在重复元素
    java给List<String>批量赋值方法
    (转)post请求携带cookie时配置跨域问题(withCredentials设置)
    redis远程连接不上,配置redis远程连接
    Velocity判断是否为空(Velocity基本语法)
    mysql设置权限,添加远程访问用户
    java 接收邮件时附件中文乱码问题
    JAVA AES加解密问题(解密时出错)
    om.baomidou.mybatisplus.core.exceptions.MybatisPlusException: 该模式不能应用于非数据库字段!
  • 原文地址:https://www.cnblogs.com/gqx-html/p/12516404.html
Copyright © 2011-2022 走看看