最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mouse coordinates in Internet Explorer - Stack Overflow

programmeradmin1浏览0评论

I have a map for my game, I have a script that on a click displays an alert of the mouse coordinates on the map.

The map scale is 1 map unit to 2.5 pixels and the map starts at -600, 600 and goes down to 600, 1700. Thus I can't simply throw out the pixels of the mouse.

I got it working (and was pretty happy about it) but alas IE (6) has issues. I have narrowed it down to IE not correctly getting the scroll parameters.

Here is the relevant code that is glitching but the full code is located at .html

tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;

Thanks for any help

I have a map for my game, I have a script that on a click displays an alert of the mouse coordinates on the map.

The map scale is 1 map unit to 2.5 pixels and the map starts at -600, 600 and goes down to 600, 1700. Thus I can't simply throw out the pixels of the mouse.

I got it working (and was pretty happy about it) but alas IE (6) has issues. I have narrowed it down to IE not correctly getting the scroll parameters.

Here is the relevant code that is glitching but the full code is located at http://woarl./map/ieMap.html

tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;

Thanks for any help

Share Improve this question asked Jan 14, 2009 at 15:35 TeifionTeifion 111k76 gold badges164 silver badges196 bronze badges 1
  • How to make it so that IE doesn't go wrong :P – Teifion Commented Jan 14, 2009 at 16:27
Add a ment  | 

2 Answers 2

Reset to default 4

Try:

tempX = event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft);
tempY = event.clientY + (document.documentElement.scrollTop || document.body.scrollTop);

Checked your page, and the DOCTYPE is putting IE in standards mode, so the scrollXXX properties you want are actually in document.documentElement, not document.body.

Mouse coordinate locations are horrible, due to the specs not noting whether they ought to be relative to the document or the viewpane, amongst other things. There's a good description of the problem, as well as an example of script that should work across all browsers, at the bottom of http://www.quirksmode/js/events_properties.html.

In particular it looks like you need to add document.documentElement.scroll(Left|Top) as well as the event and document.body parameters.

发布评论

评论列表(0)

  1. 暂无评论