Javascript。带反应器的可拖动div

Javascript。带反应器的可拖动div,javascript,html,css,reactjs,Javascript,Html,Css,Reactjs,我试图做一个拖拉的div,但我遇到了混乱的行为。这个解决方案是从w3schools复制的,它对他们来说是正确的,但对我来说不是。 轻轻移动鼠标,div始终向左移动,即使鼠标向上或向下移动,并且只有向右大幅度移动时,div才跟随光标 div.js constructor(props){ super(props) this.state = { x: this.props.x, y: this.props.y, } this.dragMo

我试图做一个拖拉的div,但我遇到了混乱的行为。这个解决方案是从w3schools复制的,它对他们来说是正确的,但对我来说不是。 轻轻移动鼠标,div始终向左移动,即使鼠标向上或向下移动,并且只有向右大幅度移动时,div才跟随光标

div.js

constructor(props){
    super(props)
    this.state = {
        x: this.props.x,
        y: this.props.y,
    }
    this.dragMouseDown = this.dragMouseDown.bind(this)
    this.elementDrag = this.elementDrag.bind(this)
    this.closeDragElement = this.closeDragElement.bind(this)
    this.reff = React.createRef()
}

componentDidMount(){
    this.pos1 = 0
    this.pos2 = 0
    this.pos3 = 0
    this.pos4 = 0
}

dragMouseDown(e) {
    e.preventDefault()
    this.pos3 = e.clientX
    this.pos4 = e.clientY
    document.onmouseup = this.closeDragElement
    document.onmousemove = this.elementDrag
}

elementDrag(e) {
    e.preventDefault()
    this.pos1 = this.pos3 - e.clientX
    this.pos2 = this.pos4 - e.clientY
    this.pos3 = e.clientX
    this.pos4 = e.clientY
    this.setState({
        y:(this.reff.current.offsetTop - this.pos2) + "px",
        x:(this.reff.current.offsetLeft - this.pos1) + "px",
    })
}

closeDragElement() {
    document.onmouseup = null
    document.onmousemove = null
}

render(){
    return (
        <div className="tech row align-items-center justify-content-center border rounded"
             style={{left: this.state.x, top: this.state.y}}
             onMouseDown={this.dragMouseDown}
             ref={this.reff}
        >
                <img className="technology-icon" src={image} alt="technology_logo"></img>
                <span className="ml-1">{this.props.name}</span>
        </div>
    )
}
和html(行-div组件的数组)

问题出在“技术”课的“行”课上。去掉它,一切都开始正常工作。哈哈

<div className="tech border rounded"

我在react上也看到了这个例子。我的问题是有点拖拉,div超出了屏幕范围(由于巨大的位移)
<div id="app"  style="height: 100%">
  <div className="fluid-container" style={{height: "100%"}}>
    <nav className="navbar navbar-dark bg-dark" id="nav">
        <a className="navbar-brand" href="#">Navbar</a>
    </nav>
    <div className="t_d">
            {rows}
    </div>
</div>
</div>
this.setState({
        y: e.clientY - this.height + this.td.scrollTop + "px",
        x: e.clientX + this.td.scrollLeft + "px"
    })
<div className="tech border rounded"