css React.js将滚动条保持在底部

gz5pxeao  于 12个月前  发布在  React
关注(0)|答案(2)|浏览(112)

我有一个div,它以动画的形式增长高度。我希望窗口自动滚动div高度,而不是扩展到可视区域之外(用户不得不向下滚动)。将滚动位置锁定在页面底部将有效。
!!这是React!!
我已经尝试了数百万个谷歌/SO的答案,没有一个工作/不够具体。
代码https://github.com/coryb08/corydbakernpm install && npm start

qltillow

qltillow1#

您提供的信息很少,但由于我们知道它是在React中,因此您可以使用JavaScript来确保您的div始终滚动到底部。

class FullMenu extends Component {
  constructor() {
    super()
    this.state = {
      class: "",
      div: ""
    }
    this.scrollToBottom = this.scrollToBottom.bind(this);
  }
  componentDidMount() {
    setInterval(this.scrollToBottom, 20);
  }
  scrollToBottom() {
    var scrollingElement = (document.scrollingElement || document.body); /* you could provide your scrolling element with react ref */
    scrollingElement.scrollTop = scrollingElement.scrollHeight;
  }
  render() {
    return (
      <div id="FullMenu">
        {this.state.div}
        <div id="triangleDiv">
          <img
            className={this.state.class}
            onClick={() => {
              this.setState({ class: "bounce" })
              let that = this
              setTimeout(function() {
                that.setState({
                  class: "",
                  div: <div className="menuDiv" />
                })
              }, 1000)
            }}
            src={triangle}
            id="triangle"
          />
        </div>
      </div>
    )
  }
}

请注意,上述解决方案使窗口始终滚动。如果你只想在动画过程中滚动它,那么你应该使用react的CSSTransitionGroup,并在transitionEnd生命周期钩子中使用clearInterval来停止这种行为。

lmvvr0a8

lmvvr0a82#

您可以单独使用CSS,您所要做的就是设置div样式

display: flex;
flex-direction: column-reverse

这应该翻转div滚动并将其定位在底部

相关问题