How can I scroll a div to be visible in ReactJS?

前端 未结 9 1526
梦如初夏
梦如初夏 2020-12-04 15:25

I have a popup list which is a div that contains a vertical list of child divs. I have added up/down keyboard navigation to change which child is

相关标签:
9条回答
  • 2020-12-04 16:08

    Another example which uses function in ref rather than string

    class List extends React.Component {
      constructor(props) {
        super(props);
        this.state = { items:[], index: 0 };
        this._nodes = new Map();
    
        this.handleAdd = this.handleAdd.bind(this);
        this.handleRemove = this.handleRemove.bind(this);
       }
    
      handleAdd() {
        let startNumber = 0;
        if (this.state.items.length) {
          startNumber = this.state.items[this.state.items.length - 1];
        }
    
        let newItems = this.state.items.splice(0);
        for (let i = startNumber; i < startNumber + 100; i++) {
          newItems.push(i);
        }
    
        this.setState({ items: newItems });
      }
    
      handleRemove() {
        this.setState({ items: this.state.items.slice(1) });
      }
    
      handleShow(i) {
        this.setState({index: i});
        const node = this._nodes.get(i);
        console.log(this._nodes);
        if (node) {
          ReactDOM.findDOMNode(node).scrollIntoView({block: 'end', behavior: 'smooth'});
        }
      }
    
      render() {
        return(
          <div>
            <ul>{this.state.items.map((item, i) => (<Item key={i} ref={(element) => this._nodes.set(i, element)}>{item}</Item>))}</ul>
            <button onClick={this.handleShow.bind(this, 0)}>0</button>
            <button onClick={this.handleShow.bind(this, 50)}>50</button>
            <button onClick={this.handleShow.bind(this, 99)}>99</button>
            <button onClick={this.handleAdd}>Add</button>
            <button onClick={this.handleRemove}>Remove</button>
            {this.state.index}
          </div>
        );
      }
    }
    
    class Item extends React.Component
    {
      render() {
        return (<li ref={ element => this.listItem = element }>
          {this.props.children}
        </li>);
      }
    }
    

    Demo: https://codepen.io/anon/pen/XpqJVe

    0 讨论(0)
  • 2020-12-04 16:08

    With reacts Hooks:

    1. Import
    import ReactDOM from 'react-dom';
    import React, {useRef} from 'react';
    
    1. Make new hook:
    const divRef = useRef<HTMLDivElement>(null);
    
    1. Add new Div
    <div ref={divRef}/>
    
    1. Scroll function:
    const scrollToDivRef  = () => {
        let node = ReactDOM.findDOMNode(divRef.current) as Element;
        node.scrollIntoView({block: 'start', behavior: 'smooth'});
    }
    
    0 讨论(0)
  • 2020-12-04 16:11

    In you keyup/down handler you just need to set the scrollTop property of the div you want to scroll to make it scroll down (or up).

    For example:

    JSX:

    <div ref="foo">{content}</div>

    keyup/down handler:

    this.refs.foo.getDOMNode().scrollTop += 10

    If you do something similar to above, your div will scroll down 10 pixels (assuming the div is set to overflow auto or scroll in css, and your content is overflowing of course).

    You will need to expand on this to find the offset of the element inside your scrolling div that you want to scroll the div down to, and then modify the scrollTop to scroll far enough to show the element based on it's height.

    Have a look at MDN's definitions of scrollTop, and offsetTop here:

    https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

    https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop

    0 讨论(0)
提交回复
热议问题