我尝试使用mousemove获取相对于元素的鼠标位置.它工作正常,但当鼠标悬停在子元素上时,坐标相对于该子元素.我希望鼠标位置相对于父div,而不是孩子.
例如,请参阅此JSFiddle.
var object = document.getElementsByClassName('object'),
scene = document.getElementById('scene');
function initMove() {
for(var i=0; i<object.length; i++) {
object[i].addEventListener("mousemove", function(event) {
//event.stopPropagation();
return false;
}, false);
}
scene.addEventListener("mousemove", function (event) {
//event.stopPropagation();
//event.currentTarget;
moveX = event.offsetX;
moveY = event.offsetY;
console.log(moveX + ' + ' + moveY);
}, false);
}
function init() {
initMove();
document.onselectstart = function(){ return false; }
};
init();?
Run Code Online (Sandbox Code Playgroud)
添加event.stopPropagation()子返回没有数据.而且我不确定它是如何event.currentTarget工作的.
我不能使用mouseenter或其他所有鼠标只控制,因为我希望它是触摸友好的(通过touchmove替换mousemove).
有任何想法吗?
有没有办法让鼠标位置相对于它的父元素?
假设我有一个结构:
<div id="parent">
<span class="dot"></span>
</div>
Run Code Online (Sandbox Code Playgroud)
当我将鼠标放在span元素上时,我需要获得相对于其父元素(<div id="parent">)的位置.PageX/ClientX给我相对于页面/客户区的位置,所以它对我不起作用.
有什么办法在javascript中检查网页是否已垂直滚动?专门用于Internet Explorer?我需要在IE中获取鼠标位置但是使用jQuery事件e.pageY它在页面未滚动时给出正确的值但是当页面向下滚动时它会给出错误的位置.
有没有办法绑定到XAML文件中的WPF中的鼠标位置?或者这必须在代码中完成吗?我在Canvas中有一个控件,我只想让控件在鼠标光标位于Canvas中时跟随鼠标.
谢谢
编辑:
好吧,我认为使用代码隐藏文件相对简单.我在Canvas上添加了一个MouseMove事件处理程序,然后添加:
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
// Get the x and y coordinates of the mouse pointer.
System.Windows.Point position = e.GetPosition(this);
double pX = position.X;
double pY = position.Y;
// Sets the position of the image to the mouse coordinates.
myMouseImage.SetValue(Canvas.LeftProperty, pX);
myMouseImage.SetValue(Canvas.TopProperty, pY);
}
Run Code Online (Sandbox Code Playgroud)
使用http://msdn.microsoft.com/en-us/library/ms746626.aspx作为指导.
我有两个表单,我的主要表单是Form1,我的辅助表单是按需显示的,因为对话框是Form2.现在,如果我调用Form2,它将始终显示在屏幕的左上角.我第一次认为我的表格根本不存在,但后来我看到它悬挂在屏幕的上方.我想在当前鼠标位置显示我的表单,用户单击上下文菜单以显示模式对话框.我已经尝试过不同的东西并搜索代码示例.但是我发现除了数以千计的不同代码之外什么也没有找到如何以我已经知道的不同方式获得实际的鼠标位置.但无论如何,这个位置总是相对于屏幕,主要形式,控制或任何当前上下文.在这里我的代码(我也尝试过的桌面定位不起作用,中心到屏幕只是表单的中心,所以我把属性留给了Windows.Default.Position):
Form2 frm2 = new Form2();
frm2.textBox1.Text = listView1.ToString();
frm2.textBox1.Tag = RenameFile;
DialogResult dlgres=frm2.ShowDialog(this);
frm2.SetDesktopLocation(Cursor.Position.X, Cursor.Position.Y);
Run Code Online (Sandbox Code Playgroud) 是否可以在图像的鼠标事件中获得准确的鼠标位置?如果我使用一个更新文档鼠标移动事件鼠标位置的功能我可能有延迟和这种事情的问题,并不会得到完全的位置
我试图让鼠标在面板中的位置,如面板的左上角= x/y 0,0.
我现在拥有的是整个屏幕上的位置,因此根据面板(在一个框架中)在屏幕上的位置,坐标是不同的.我想你可以添加到x/y坐标来解释这一点,但这似乎是一个混乱的解决方案.有人可以帮忙吗?
这是我正在使用的mouseListener,它已被添加到面板中.
private class MouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
// Finds the location of the mouse
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
// Gets the x -> and y co-ordinates
int x = (int) b.getX();
int y = (int) b.getY();
System.out.println("Mouse x: " + x);
System.out.println("Mouse y: " + y);
// Determines which tile the click occured on
int xTile = x/tileSize;
int yTile = y/tileSize;
System.out.println("X Tile: " + xTile); …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决一个问题,我需要根据点击鼠标的位置在JEditorPane中的一段文本中找到位置.
基本上,当用户右键单击一个单词时,我需要找出单词是什么.为此,我需要找出用户点击的文本中的哪个位置.我知道我可以轻松地从MouseEvent获取鼠标位置,该鼠标位置传递给mousePressed方法,我被告知你可以转换它来获取文本中的字符索引 - 但是我无法弄清楚怎么做这个.
我已经尝试过JEditorPane上的viewToModel()方法但是这会让我回到文本中错误的位置,所以要么我使用它错了要么它不能以这种方式工作.
有任何想法吗?
我发现了许多关于如何在C#windows 窗体项目中设置鼠标位置的文章- 我想在控制台应用程序中执行此操作.如何从C#windows 控制台应用程序设置绝对鼠标位置?
谢谢!
提示:它不是Console.setCursorPosition,它只在控制台中设置文本光标的位置.
我现在正在搞乱HTML5,我想编写一些浏览器游戏 - 只是为了好玩.我创建了一个加载背景图像的画布 - 它是一种六边形网格.要找出单击哪个六边形,我使用圆圈作为其中心/半径.我解释一下,因为这样你可以更快地理解我发布的代码.
这一切都很好,直到我使用滚动条 - 然后一切都搞砸了.似乎我没有得到正确的坐标.因为网格有点大,我必须使用滚动条,特别是在较小的屏幕上.
所以这是代码
网站
<html>
<head>
<script type="text/javascript" src="worldmap.js"></script>
<script type="text/javascript" src="worldmapMouseEvents.js"></script>
</head>
<body onload="draw()">
Xcoord: <div id="xcoord">0</div>
Ycoord: <div id="ycoord">0</div><br>
Spalte: <div id="column">0</div><br>
Reihe: <div id="row">0</div><br>
<canvas id="canvas"
width="1100"
height="1100"
style="border: 1px solid black;"
onmousemove="getMousePosition()"
onmousedown="getCell()">
</canvas>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
显示网格的文件worldmap.js
function draw(){
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var img = new Image();
img.onload = function(){
ctx.drawImage(img,0,0);
ctx.stroke();
};
img.src = './pics/grid.jpg';
}
Run Code Online (Sandbox Code Playgroud)
在这里,更多的源文件worldmapMouseEvents.js在滚动后表现得很奇怪
function getMousePosition() {
var x = new Number();
var y = …Run Code Online (Sandbox Code Playgroud)