【发布时间】:2019-12-03 18:13:36
【问题描述】:
jsfiddle:https://jsfiddle.net/annahisenberg/ft10ersb/34/
现在我正在这样做:
<div
id="more_options_popup"
style={{
left: this.reff.current.offsetLeft - 140 + "px",
top: this.reff.current.offsetTop - 150 + "px"
}}
>
但是有没有更动态的方法来做到这一点?因为这种方式在我的实际项目中不起作用。如果我将offsetTop 更改为- 130,它将使它更接近,但是如果我在该弹出窗口中有更多项目,它就离我的按钮太近了。如果我的弹出窗口中只有一个<p> 标签,它离我的按钮太远,但如果我有多个<p> 标签,它离我的按钮太近。
由于某种原因,我无法在 jsFiddle 中显示它。
反应代码:
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() {
const { showHelpModal, documents, showMoreOptionsPopup, x, y } = this.state;
const { menuNameHelp } = this.props;
return (
/* jshint ignore:start */
<div>
{showMoreOptionsPopup && (
<div
id="more_options_popup"
style={{
left: this.reff.current.offsetLeft - 140 + "px",
top: this.reff.current.offsetTop - 150 + "px"
}}
>
<div className="help-popup-grid">
<p>Help</p>
<p>Contact</p>
</div>
</div>
)}
<a
id="more_options_button"
className={showMoreOptionsPopup ? 'open' : null}
onClick={this.showMoreOptionsPopup}
style={{ left: x, top: y }}
onMouseDown={this.dragMouseDown}
ref={this.reff}
>
<div></div>
</a>
</div>
/* jshint ignore:end */
);
}
}
ReactDOM.render(
/* jshint ignore:start */
<Drag />,
document.querySelector("#app")
/* jshint ignore:end */
)
CSS:
#more_options_button {
width: 1%;
position: absolute;
right: 0;
bottom: 155px;
right: 60px;
width: 60px;
height: 43px;
width: 43px;
border-radius: 50% !important;
background-color: black;
border: 1px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 9999;
color: white;
}
#more_options_popup {
position: absolute;
text-align: right;
z-index: 9999;
width: 10%;
border: 1px solid black;
border-radius: 10%;
padding-top: 1rem;
background: white;
z-index: 9999;
}
【问题讨论】:
-
使用蚂蚁设计#antd
标签: javascript css reactjs drag-and-drop