【问题标题】:React scroll component to view inside overflow hidden element反应滚动组件以查看溢出隐藏元素内部
【发布时间】:2018-12-26 11:15:11
【问题描述】:

我有一个包含一系列项目的列表,但我无法让它滚动另一个项目进入视图。 this 之类的解决方案不太管用,因为我必须上下滚动(基本上如果列表太大,请间隔滚动以显示所有项目)。

我做了一个codepen 来说明 react-scroll-to-component 的问题,但我对任何库/本机解决方案持开放态度。基本功能只是scrollToComponent(ref, <options>)

据我所知,错误是它试图在正文上滚动,而不是在实际容器上滚动。如果你删除了 div 的高度/溢出属性,它会起作用。

不起作用:

<div
    style={{
    height: `200px`,
    overflow: "hidden"
    }}
>
    {this.state.items.map(item => (
    <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
    ))}
</div>

作品:

<div>
    {this.state.items.map(item => (
    <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
    ))}
</div>

任何不想去 codepen 的人的完整代码:

class Item extends React.Component {
  render() {
    return <div style={{ padding: `12px` }}>Item {this.props.item}</div>;
  }
}

class List extends React.Component {
  state = {
    items: [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20
    ]
  };

  componendDidMount() {
    this.state.items.forEach(item => (this[`ref_${item}`] = React.createRef()));
  }

  onClick = () => {
    scrollToComponent(this.ref_20);
  };

  render() {
    const { items } = this.state;
    return (
      <div>
        <h1>My List</h1>
        <button onClick={this.onClick}>Clickidy</button>
        {/*Replace with <div> to see it work*/}
        <div
          style={{
            height: `200px`,
            overflow: "hidden",
            backgroundColor: "red"
          }}
        >
          {this.state.items.map(item => (
            <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
          ))}
        </div>
      </div>
    );
  }
}

【问题讨论】:

标签: javascript reactjs scroll


【解决方案1】:

将第 55 行从 overflow: hidden 更改为 overflow: scroll

【讨论】:

【解决方案2】:

我将为您提供一个简单的解决方法,但这就是它的工作原理。 您需要将包含所有元素的 div 的溢出设置为scroll。这是因为如果您将其设置为隐藏,则所有不适合该 div 的元素都会被隐藏。我给你的解决方案甚至不需要使用react-scroll-to-component 或任何包。

import React from "react";
import { render } from "react-dom";

class Item extends React.Component {
  render() {
    const { item } = this.props;
    return (
      <div style={{ padding: `12px` }} id={item}>
        Item {item}
      </div>
    );
  }
}

class List extends React.Component {
  state = {
    items: [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20
    ]
  };


  onClick = () => {
    /** You can avoid using getElementById if you can get the div rendered by Item component using refs.
     * You can look at refs forwarding and other technics to see how you can solve this */
    const divToScrollTo = document.getElementById(`${this.ref_20.props.item}`);
    if (divToScrollTo) {
      divToScrollTo.scrollIntoView();
    }
  };

  render() {
    const { items } = this.state;
    return (
      <div>
        <h1>My List</h1>
        <button onClick={this.onClick}>Clickidy</button>
        <div
          style={{
            height: `200px`,
            overflow: "scroll",
            backgroundColor: "red"
          }}
        >
          {this.state.items.map(item => (
            <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
          ))}
        </div>
      </div>
    );
  }
}

或者您可以点击下面的链接获取代码笔解决方案 https://codesandbox.io/s/2vo0j2w660

我没有在你的代码中做更多的优化,因为我时间不好,但希望这会有所帮助

如果您使用 refs 转发,如代码笔所示 https://codesandbox.io/s/yjj66xl2v1 ,则更容易,但您必须在构造函数方法中创建 refs `

【讨论】:

  • 工作就像一个魅力。似乎是 ref'ing 实际上是问题孩子。 overflow:scroll 并不是真正需要的。感谢您的帮助并接受我的投票!
  • 是的,不是,但我认为您可能还需要用户滚动列表。问题在于您使用和创建参考的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多