我想知道如何获得HTML元素的X和Y位置,例如img和divJavaScript.
我有一个聊天小部件,每次我向上滚动时都会提取一系列消息.我现在面临的问题是,当消息加载时,滑块保持固定在顶部,我希望它专注于前一个数组的最后一个索引元素.我发现我可以通过传递索引来制作动态引用,但我还需要知道使用什么样的滚动函数来实现
handleScrollToElement(event) {
const tesNode = ReactDOM.findDOMNode(this.refs.test)
if (some_logic){
//scroll to testNode
}
}
render() {
return (
<div>
<div ref="test"></div>
</div>)
}
Run Code Online (Sandbox Code Playgroud) 我正在使用带有React的Typescript.我无法理解如何使用refs以获得关于refs引用的react节点的静态类型和intellisense.我的代码如下.
import * as React from 'react';
interface AppState {
count: number;
}
interface AppProps {
steps: number;
}
interface AppRefs {
stepInput: HTMLInputElement;
}
export default class TestApp extends React.Component<AppProps, AppState> {
constructor(props: AppProps) {
super(props);
this.state = {
count: 0
};
}
incrementCounter() {
this.setState({count: this.state.count + 1});
}
render() {
return (
<div>
<h1>Hello World</h1>
<input type="text" ref="stepInput" />
<button onClick={() => this.incrementCounter()}>Increment</button>
Count : {this.state.count}
</div>
);
}}
Run Code Online (Sandbox Code Playgroud)