JS 获取屏幕中鼠标的坐标值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 获取屏幕中鼠标的坐标值相关的知识,希望对你有一定的参考价值。

先看下这段代码:

<html>
<head>
<script language="javascript">
function xy()
x.innerHTML=event.screenX
y.innerHTML=event.screenY

document.onmousemove=xy
</script>
</head>

<body>
<table cellspacing="0" cellpadding="0">
<tr bgcolor="#ffffff">
<td align="right">X:</td>
<td id="x"> </td>
</tr>
<tr bgcolor="#ffffff">
<td align="right">Y:</td>
<td id="y"> </td>
</tr>

</table>
</body>
</html>
为什么鼠标在body区域内,能获取坐标值,而出了body区域就获取不到了呢?是不是不能用document?
是用window.onmousemove=xy这个吗?可是还不行?

<title>Js获取适时获取鼠标坐标值并显示</title>
<script type="text/javascript">
var getCoordInDocumentExample = function()
var coords = document.getElementById("coords");
coords.onmousemove = function(e)
var pointer = getCoordInDocument(e);
var coord = document.getElementById("coord");
coord.innerHTML = "X,Y=("+pointer.x+", "+pointer.y+")";


var getCoordInDocument = function(e)
e = e || window.event;
var x = e.pageX || (e.clientX +
(document.documentElement.scrollLeft
|| document.body.scrollLeft));
var y= e.pageY || (e.clientY +
(document.documentElement.scrollTop
|| document.body.scrollTop));
return 'x':x,'y':y;

window.onload = function()
getCoordInDocumentExample();
;
</script>
<div id="coords" style="width:500px;height:200px;background:#F2F1D7;border:2px solid #0066cc;">
请在此移动鼠标。
</div>
<br />
<div id="coord" style="width:500px;border:2px solid #336699;"> </div>
参考技术A document.onmousemove=xy

你明明只绑定到 HTML 文档对象(document)中的嘛!当然只会在浏览器可视区取到坐标咯!

不过你还想获取可视区以外的,那你得想办法把事件绑到windows的屏幕上去!

注意:我是说的windows系统,不是window对象!具体方法我也没找到!本回答被提问者采纳

matlab 鼠标指针坐标怎样显示

用matlab编写界面程序时想实现一个功能:当鼠标在窗口滑动时在其周围能显示当先鼠标的坐标位置。请各位大侠不吝赐教!!先谢过了!!

MATLAB如何动态显示鼠标的坐标值和图像像素值

对于动态显示鼠标的坐标值和像素值,在其他语言如vc,vb中都比较方便,有直接的着方面的函数,那么在matlab图像处理里面又如何实现呢?
具体的实现方法很多,但归结起来就是获取坐标轴的current point 属性值,我这里给出的一个函数是从mathworks 获取柄稍作修改后的结果,相信对做图像处理的朋友有一定的作用。另一个就是自带的pixval函数。谁有不同的实现方法,请多多共享啊!

function dynpoint(arg,h)
% Show the coordinates of a plot dynamically
%
% To start use:
% dynpoint(h)
% where h is a handle to a figure, axes or e.g. line.
%
% To delete use:
% dynpoint('delete',h)
% where h is a handle to a figure, axes or e.g. line.
% (you may also use: dynpoint delete)
%
% There can only be one dynamic plotter in a figure at a time.
%
% Example:
% subplot(211), hline = plot(sin(1:10))
% subplot(212), plot(sin(1:100))
% dynpoint(hline)

% 2002,6.29

if ~exist('arg','var')
arg = gcf;
end

if ~isstr(arg)
handle = arg;
arg = 'init';
end

switch arg
case 'init'
if ~ishandle(handle)
error('h is not a handle')
end

[h,ax] = h2hax(handle);

% delete old dynamic text object
ht = findobj(h,'tag',[mfilename '_text']);
if any(ht)
delete(ht)
end

% text window at the bottom left corner
% text in centred
uicontrol(h,...
'style','text',...
'pos',[2 2 200 15],...
'tag',[mfilename '_text'],...
'userdata',ax(1))

% do the dynamic thing...
set(h,'windowbuttonmotionfcn',[mfilename ' move'])

case 'move'
ht = findobj(gcbf,'tag',[mfilename '_text']);
ax = overobj('axes');
if ~any(ax)
ax = get(ht,'userdata');
end
p = get(ax,'currentpoint');
set(ht,'string',sprintf('(%g, %g)', p(1), p(3)));

case 'delete'
if ~exist('h','var')
h = gcf;
end
[h,ax] = h2hax(h);
set(h,'windowbuttonmotionfcn','')

ht = findobj(h,'tag',[mfilename '_text']);
delete(ht)

end

% ----------
function [h,ax]=h2hax(handle)

typ = get(handle,'type');
if strcmp(typ,'figure')
h = handle;
ax = findobj(h,'type','axes');
elseif strcmp(typ, 'axes')
h = get(handle,'parent');
ax = handle;
elseif strcmp( get(get(handle,'parent'), 'type'), 'axes' )
ax = get(handle,'parent');
h = get(ax,'parent');
end
参考技术A

用imtool函数显示

function main()
h0 = figure;
h1 = imshow('1.jpg');
h2 = uicontrol('style','text','Position',[30 15 100 15],'string','non');
set(h1,'ButtonDownFcn',@clicky);
function clicky(varargin) 
a=get(gca,'Currentpoint');
set(findobj('style','text'),'String',strcat('x:',num2str(a(1,1)),'y:',num2str(a(1,2))));    

以上是关于JS 获取屏幕中鼠标的坐标值的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript基础 event(For IE) 显示鼠标点击处的坐标值 距离窗口的距离 距离网页的距离 距离屏幕的距离

js中sortable怎么获取拖动的东西

时间对象,键盘事件,鼠标跟随

delphi中如何获得当前屏幕photoshop选择区域右上角的坐标值

matlab 鼠标指针坐标怎样显示

matlab画图时如何获取指定点的坐标值?