admin管理员组文章数量:1399466
I get mouse coordinates on some web page and save them.
$("div#container").mousemove( function(e) {
client_x = e.pageX;
client_y = e.pageY;
// save x,y
});
Now other person load that same page and i want to show them the same coordinates (x,x position).
How can I get the same point if I have to take in consideration that the div#container is not at same position as it was in my browser (considering screen resolution and scroll)?
I get mouse coordinates on some web page and save them.
$("div#container").mousemove( function(e) {
client_x = e.pageX;
client_y = e.pageY;
// save x,y
});
Now other person load that same page and i want to show them the same coordinates (x,x position).
How can I get the same point if I have to take in consideration that the div#container is not at same position as it was in my browser (considering screen resolution and scroll)?
Share Improve this question asked Aug 27, 2012 at 13:05 GaleGale 39913 silver badges27 bronze badges 2- Same point relative to #container or browser window? – David Hellsing Commented Aug 27, 2012 at 13:07
- I think is better relative to container, because container can move left, right if screens resolution change – Gale Commented Aug 27, 2012 at 13:10
3 Answers
Reset to default 6I would use $.offset().top
and $.offset().left
of the parent div container, and calculate the difference from that to the current X and Y coordinates of the mouse cursor.
.offset()
always refers to the document and not to the parent of the element.
For example:
$('#element').mousemove(function(e){
var client_x = e.pageX;
var client_y = e.pageY;
var elementOffset = $(this).offset();
client_x -= elementOffset.left;
client_y -= elementOffset.top;
// save x, y.
});
Then, on the other users display, show the coordinates after adding them to his offsets.
This doesn't seem possible because of the variables you mentioned in the question. Screen resolution is the main reason, but, also, it depends on how big their window is. At first, you might think that you could pute the mouse's position relative to fixed points, like div
s shown (take Stack Overflow, for example, where the main container of the site doesn't resize with the browser window). But if their window is smaller than the container, you would be making some false assumptions about what they see.
That being said, you can always just pute the mouse position relative to fixed elements you know will be on the screenusing $.offset()
and just assume they have their screen showing everything (or check $(window)
size) and are using "normal" viewing conditions.
You can use the values returned by offset()
, in your example:
$("div#container").offset().left
and
$("div#container").offset().top
to substract them to e.pageX
and e.pageY
.
offset()
function gives you the matched element's position relative to the document (see the docs), so there's no problem if the users scroll down.
Example: http://jsfiddle/3jMRS/
本文标签: javascriptMouse position and offsetStack Overflow
版权声明:本文标题:javascript - Mouse position and offset - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744193696a2594635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论