I have a menu that opens when I right-click on the table.
The problem is that I could not get the mouse position when the page has scroll.
I noticed you're getting Y X based on my monitor, and I need to get it based on the size of the page.
Does anyone know how to solve this?
Follow the code in jsfiddle to make it easier, because I've already configured the larger page to get the bug.
document.oncontextmenu = function() {
return false;
};
// Verifica se abre o menu
$("tr").mousedown(function (e) {
// Define a posição do menu
$('.context_menu_pai').css({
"margin-left": e.clientX,
"margin-top": e.clientY
}).show();
// Verifica qual botão clicou
if (e.button === 2) {
$("#menu" + this.id).show();
$(".context_menu_pai:not(#menu" + this.id + ")").hide();
// Adiciona CSS na linha selecionada
$("#" + this.id).addClass('context_menu_select');
} else {
// Fecha menu
$(".context_menu_pai").hide();
}
});
body {
margin: 0;
color: #484848;
font-family: Verdana;
font-size: 13px;
}
.context_menu_pai {
display: none;
position: absolute;
width: 200px;
background: #FFFFFF;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5);
border-radius: 2px;
}
.context_menu {
padding: 12px 8px;
cursor: pointer;
display: block;
color: #484848;
}
.context_menu:hover {
background: #EEEEEE;
}
.text_erro {
color: #F65314;
}
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><divclass='context_menu_pai'id='menu2'><liclass='context_menu'>Editar</li><liclass='context_menu'>Acessar</li><liclass='context_menu'>Indicou(0)</li><liclass='context_menutext_erro'>Bloquear</li></div><tablewidth="100%" border="1">
<tr id="2">
<td>ID:2</td>
<td>Nome</td>
<td>Idade</td>
</tr>
</table>