admin管理员组文章数量:1397086
I have a table of cells, and I want to be able to create a menu on the point of right click. I have an example of my fiddle here: jsFiddle
Now the ContextMenu
class is just a div that I created for this example. As you can see there are no borders to form the actual menu. Now how can I make this menu display at the position my mouse is when I click on one of these cells? Right now it displays under the current table div.
Do I need to mount the menu div under ponentWillMount()
?
I have a table of cells, and I want to be able to create a menu on the point of right click. I have an example of my fiddle here: jsFiddle
Now the ContextMenu
class is just a div that I created for this example. As you can see there are no borders to form the actual menu. Now how can I make this menu display at the position my mouse is when I click on one of these cells? Right now it displays under the current table div.
Do I need to mount the menu div under ponentWillMount()
?
1 Answer
Reset to default 8onContextMenu
method gives the location of mouse click as e.pageX (left)
and e.pageY (top)
. Based on this coordinates position the context menu absolutely.
Also, instead of coupling ContextMenu ponent's behavior with parent's state, display the ContextMenu ponent always (but hidden). Something like this
var ContextMenu = React.createClass({
getInitialState: function(){
return {contextMenuLocation: ''};
},
render: function() {
var location = this.state.contextMenuLocation;
var contentMenuStyle = {
display: location ? 'block' : 'none',
position: 'absolute',
left: location ? location.x : 0,
top: location ? location.y : 0
};
return (
<div id="results" style={contentMenuStyle}>
Menu here
</div>
);
}
});
Parent ponent's render would look like this
render: function(){
return (<div>
.....
<ContextMenu ref="contextMenu" />
</div>);
}
Maintain a reference to contextMenu in parent ponent
ponentDidMount: function(){
this.contextMenu = this.refs.contextMenu;
}
Display it like this
_onContextMenu: function(e) {
this.contextMenu.setState({
contextMenuLocation: {'x' : e.pageX, 'y': e.pageY}
});
return false;
}
Here is the updated demo http://jsfiddle/dhirajbodicherla/kb3gN/11383/
本文标签: javascriptDisplay a div at the position of mouse clickStack Overflow
版权声明:本文标题:javascript - Display a div at the position of mouse click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744112225a2591341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论