【发布时间】:2019-11-25 16:55:36
【问题描述】:
此外,当我在更大的屏幕上查看 div 时,它的位置也会发生变化。如果我检查它在更大屏幕上的外观,div 的位置会完全改变。
jsfiddle:https://jsfiddle.net/annahisenberg/ft10ersb/20/
查看全屏:https://jsfiddle.net/annahisenberg/ft10ersb/20/show
代码:
class Drag extends React.Component {
constructor(props) {
super(props);
this.state = {
x: this.props.x,
y: this.props.y,
showMoreOptionsPopup: false,
showHelpModal: false
};
this.reff = React.createRef();
this.dragMouseDown = this.dragMouseDown.bind(this);
this.elementDrag = this.elementDrag.bind(this);
this.closeDragElement = this.closeDragElement.bind(this);
this.showMoreOptionsPopup = this.showMoreOptionsPopup.bind(this);
}
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;
};
showMoreOptionsPopup() {
this.setState({
showMoreOptionsPopup: !this.state.showMoreOptionsPopup
});
};
render() {
return (
<div>
<a
id="more_options_button"
className={this.state.showMoreOptionsPopup ? 'open' : null}
onClick={this.showMoreOptionsPopup}
style={{ left: this.state.x, top: this.state.y }}
onMouseDown={this.dragMouseDown}
ref={this.reff}
>
<div>
<p>menu</p>
</div>
</a>
</div>
);
}
}
【问题讨论】:
-
无法重现,我遇到的唯一问题是我在删除
left: 95rem;top: 39rem;之前在屏幕上看不到任何内容@ -
@ZohirSalak 你用这个链接全屏看了吗? jsfiddle.net/annahisenberg/ft10ersb/20/show
-
是的,我有,我没有将它作为移动设备尝试,问题是反应无法识别
onMouseDown事件,因此它不会在移动设备上触发事件ref
标签: javascript css reactjs drag-and-drop