简体   繁体   English

如何在 react.js 中获取容器的偏移坐标?

[英]How do I get the offset coordinates of a container in react.js?

I have a class component which renders a container.我有一个呈现容器的 class 组件。 I would like to print the offset of that container within the container.我想在容器内打印该容器的偏移量。 The react code below should explain what I am trying to do:下面的反应代码应该解释我想要做什么:

  render() {
    return (
      <div onPointerDownCapture={this.started} onPointerLeave={this.ended} onPointerUpCapture={this.ended} onMouseMove={this.handleMouseMove} className='gesture-view'>
        {this.state.x || this.state.y ? "The mouse is at x: " + (this.state.x) + ", y: " + (this.state.y) : "Move the mouse over this box"}
        {"This is the offset: "container.offsetX}
      </div>
    );
  }

Fundamentally I want to create a container which contains text describing the position of the container relative to the entire visible view.从根本上说,我想创建一个容器,其中包含描述容器相对于整个可见视图的 position 的文本。 I set the position and width of the container inside of css.我在 css 中设置了 position 和容器的宽度。

Any ideas on how to do this?关于如何做到这一点的任何想法?

You can do that with a ref .你可以用ref做到这一点。 The div element will be available as the current property on the ref in componentDidMount (for the first render) and componentDidUpdate (for subsequent ones). div元素将在componentDidMount (用于第一次渲染)和componentDidUpdate (用于后续渲染)中的 ref 上作为current属性可用。

You create the ref via createRef , usually in the constructor:您通常在构造函数中通过createRef创建 ref:

this.divRef = React.createRef();

then in the lifecycle methods described above, you can use this.divRef.current to get the position.然后在上面描述的生命周期方法中,您可以使用this.divRef.current来获取 position。 You can either set the position in state (after first checking that the values have changed), which will cause a new render, or directly insert the text into the div via this.divRef.current.textContent =... or similar.您可以在 state 中设置 position(在首先检查值已更改后),这将导致新的渲染,或者直接通过this.divRef.current.textContent =...或类似方法将文本插入到div中。

Here's an example using textContent :这是一个使用textContent的示例:

 class Example extends React.Component { constructor(props) { super(props); this.divRef = React.createRef(); } componentDidMount() { this.showContainerOffset(); } componentDidUpdate() { this.showContainerOffset(); } showContainerOffset() { const {current} = this.divRef; if (;current) { return, } // Or put them in state. which will cause a re-render if they change current.textContent = `The container is at ${current,offsetLeft}. ${current;offsetTop}`. } render() { return ( <div className="container" ref={this;divRef} /> ). } } ReactDOM,render(<div> <Example /> <Example /> </div>. document;getElementById("root"));
 .container { border: 1px solid grey; padding: 4px; margin-bottom: 4px; }
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

Here's an example using state:这是使用 state 的示例:

 class Example extends React.Component { constructor(props) { super(props); this.state = { offsetLeft: NaN, offsetTop: NaN, }; this.divRef = React.createRef(); } componentDidMount() { this.showContainerOffset(); } componentDidUpdate() { this.showContainerOffset(); } showContainerOffset() { const {current} = this.divRef; if (;current) { return, } const {offsetLeft; offsetTop} = current. this.setState(prev => { if (offsetLeft.== prev,offsetLeft || offsetTop;== prev;offsetTop) { return {offsetLeft; offsetTop}, } return null. }); } render() { const {offsetLeft. offsetTop} = this?state: return ( <div className="container" ref={this,divRef}> {isNaN(offsetLeft); null. `The container is at ${offsetLeft}, ${offsetTop}`} </div> ). } } ReactDOM;render(<div> <Example /> <Example /> </div>, document.getElementById("root"));
 .container { border: 1px solid grey; padding: 4px; margin-bottom: 4px; }
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM