通過JS 獲取Mouse Position(鼠標坐標)的代碼
昨天寫的腳本在獲取鼠標位置的時候有些問題。在IE中始終當有滾動條的時候,發(fā)現(xiàn)document.body.scrollTop并沒有起到作用。
后來在google中搜索到一篇文章Mouse Cursor Position,詳細介紹了瀏覽器鼠標定位的問題。各個瀏覽器對鼠標定位的標準不一樣,就連不通版本的ie對定位支持都不一樣。
document.body.scrollLeft,document.body.scrollTop只用于IE6以前的版本,在IE6中,對沒有宣告 DOCTYPE,或者宣告的是transitional DOCTYPE,那么IE6將使用document.documentElement.scrollLeft 來獲取鼠標的絕對位置。
將Stephen Chapman提供的函數(shù)做個記錄
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
return evt.clientX + (document.documentElement.scrollLeft ?
document.documentElement.scrollLeft :
document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
return evt.clientY + (document.documentElement.scrollTop ?
document.documentElement.scrollTop :
document.body.scrollTop);
else return null;
}
Mouse Cursor Position
Join the Discussion
Questions? Comments?
Until recently there was no standard way of determining the position of the mouse cursor within the browser. The W3C standards say that the current mouse cursor position within the browser window when an event is triggered should be given by event.clientX and event.clientY to obtain the distance from the left and top of the browser window respectively.
I have tested this in a number of different browsers and have found that Internet Explorer 6, Netscape 6+, Firefox, and Opera 7+ all produce correct values for the mouse coordinates relative to the browser window in these fields. To obtain the position within the web page you would simply add the scroll position of the page within the browser window.
Opera 5 and 6 produced values for these fields but the values are relative to the top of the web page instead of relative to the browser window and so adding the scroll position would produce a wrong result with these browsers. Netscape 4 doesn't understand these fields at all and so would give an error if this code were used by itself.
One added complication is that Internet Explorer uses two different ways to determine the scroll position of the page. It uses document.body.scrollLeft and document.body.scrollTop in versions before version 6 as well as in version 6 when there is no DOCTYPE declared or a transitional DOCTYPE is declared. When IE6 is declared using a strict DOCTYPE document.documentElement.scrollLeft and document.documentElenent.scrollTop are used instead. Other browsers either use one of these values or pageXOffset and pageYOffset.
Although not part of the W3C standards there is another pair of fields that will give the position of the mouse cursor that is useful. With the exception of Internet Explorer and Opera 5 and 6, all of the browsers I have mentioned also support event.pageX and event.pageY which give the mouse cursor position relative to the top left corner of the web page itself. Using these fields you do not have to add the scroll position of the page.
By combining tests for both of these methods together we can create code to locate the mouse cursor position that will work on Internet Explorer, Netscape 4+, Firefox, and Opera 7+. You just need to pass the event to the following functions to retrieve the appropriate position on the web page.
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
return evt.clientX + (document.documentElement.scrollLeft ?
document.documentElement.scrollLeft :
document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
return evt.clientY + (document.documentElement.scrollTop ?
document.documentElement.scrollTop :
document.body.scrollTop);
else return null;
}
There are a couple of other pairs of fields that give mouse cursor positions that are less useful. The fields event.screenX and event.screenY are defined in all of the browsers I tested. They give the position of the mouse cursor relative to the top left corner of the screen. Without knowing the position of the top left corner of the browser window this information is not very useful with respect to being able to interact with the web page.
The fields event.x and event.y also exist in Netscape 4, Internet Explorer, and Opera 7+. In Netscape 4 these fields give the position within the web page exactly the same as the pageX and pageY fields. In Internet Explorer and Opera 8 they give the position of the mouse cursor within the current object (if that object is positioned absolute, relative, or fixed) or within the page (for static objects). Opera 7 appears to use these fields to give the position of the mouse cursor relative to the bottom left corner of the screen.
還要其他的情況:
調(diào)用方法:
var pos=GetObjPos(ID);
function CPos(x, y)
{
this.x = x;
this.y = y;
}
//獲取控件的位置
function GetObjPos(ATarget)
{
var target = ATarget;
var pos = new CPos(target.offsetLeft, target.offsetTop);
var target = target.offsetParent;
while (target)
{
pos.x += target.offsetLeft;
pos.y += target.offsetTop;
target = target.offsetParent
}
return pos;
}
下面是我自己開發(fā)項目中的實例:
<script type="text/jscript" language="jscript" >
function showPopup(obj,evt) {
var strDate = $(obj).attr('dateTime');
var strUserName = $(obj).attr('userName');
var id = "event_" + strDate.replace(/-/g, '');
var box = $('#'+id);
if (box.length == 0) {
$(document.body).append("<div id='" + id + "' class='popupinfo'></div>");
box = $('#' + id);
box.css("position", "absolute");
box.css("display", "block");
box.css("z-index", "100");
box.append('<input id="File1" type="image" src="../Images/Onload.gif"/>');
Microsoft.PMWeb.WebSite.SiteService.GetEventInfoByDate(strUserName + "#" + strDate, onSuccess, onFailed, "1111");
}
else {
var imgLoad = box.find(":image");
imgLoad.css("display", "none");
}
var objQueryPosition = GetObjPos(obj);
box.css("left", mousePos.x);
box.css("top", mousePos.y);
box.css("display", "");
function onSuccess(result, context, methodName) {
var imgLoad = box.find(":image");
imgLoad.css("display","none");
box.append(result);
}
function onFailed(error, context, methodName) {
var errorMessage = error.get_message();
alert("Review Failed:" + errorMessage);
}
}
function hidePopup(obj) {
var strDate = $(obj).attr('dateTime');
var strUserName = $(obj).attr('userName');
var id = "event_" + strDate.replace(/-/g, '');
var box = $('#'+id);
if (box.length != 0) {
$('#'+id).css("display", "none");
}
}
var mousePos;
function mouseMove(ev) {
ev = ev || window.event;
mousePos = mouseCoords(ev);
}
function mouseCoords(ev) {
if (ev.pageX || ev.pageY) {
return { x: ev.pageX, y: ev.pageY };
}
return {
x: ev.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft),
y: ev.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)
};
}
document.onmousemove = mouseMove;
</script>
相關(guān)文章
ECMAScript5中的對象存取器屬性:getter和setter介紹
這篇文章主要介紹了ECMAScript5中的對象屬性存取器:getter和setter介紹,事實上在除ie外最新主流瀏覽器的實現(xiàn)中,任何一個對象的鍵值都可以被getter和setter方法所取代,這被稱之為“存取器屬性”,需要的朋友可以參考下2014-12-12TypeScript條件類型與內(nèi)置條件類型超詳細講解
我們可以使用TypeScript中的條件類型來根據(jù)邏輯定義某些類型,就像是在編寫代碼那樣。它采用的語法和我們在JavaScript中熟悉的三元運算符很像:condition ? ifConditionTrue : ifConditionFalse。我們來看看他是怎么工作的2023-03-03如何使用Bootstrap的modal組件自定義alert,confirm和modal對話框
本文我將為大家介紹Bootstrap中的彈出窗口組件Modal,此組件簡單易用,效果大氣漂亮且很實用,感興趣的朋友一起學(xué)習(xí)吧2016-03-03js解析xml字符串和xml文檔實現(xiàn)原理及代碼(針對ie與火狐)
分別針對ie和火狐分別作了對xml文檔和xml字符串的解析,考慮到了瀏覽器的兼容性,至于在ajax環(huán)境下解析xml,其實原理是一樣的,只不過放在了ajax里,還是要對返回的xml進行解析,感興趣的朋友可以了解下,或許對你學(xué)習(xí)js解析xml有所幫助2013-02-02JS來動態(tài)的修改url實現(xiàn)對url的增刪查改
通過get方式提交post表單等方式來動態(tài)修改url存在諸多的不妥,因此,想到了通過JS來動態(tài)的修改url,來實現(xiàn)對url的增刪查改2014-09-09js一般方法改寫成面向?qū)ο蠓椒ǖ臒o限級折疊菜單示例代碼
本例是應(yīng)用別人的例子,原來那位老兄是用一般方法寫成的無限級折疊菜單,通過了一些簡化修改,將原來的例子改成了面向?qū)ο蟮姆绞?/div> 2013-07-07最新評論