最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過(guò)JS 獲取Mouse Position(鼠標(biāo)坐標(biāo))的代碼

 更新時(shí)間:2009年09月21日 18:09:41   作者:  
最近我發(fā)現(xiàn)在webpage中獲取空間的絕對(duì)坐標(biāo)時(shí),如果有滾動(dòng)條就會(huì)有錯(cuò),后來(lái)用無(wú)名發(fā)現(xiàn)的方法得以解決。

昨天寫(xiě)的腳本在獲取鼠標(biāo)位置的時(shí)候有些問(wèn)題。在IE中始終當(dāng)有滾動(dòng)條的時(shí)候,發(fā)現(xiàn)document.body.scrollTop并沒(méi)有起到作用。
后來(lái)在google中搜索到一篇文章Mouse Cursor Position,詳細(xì)介紹了瀏覽器鼠標(biāo)定位的問(wèn)題。各個(gè)瀏覽器對(duì)鼠標(biāo)定位的標(biāo)準(zhǔn)不一樣,就連不通版本的ie對(duì)定位支持都不一樣。
document.body.scrollLeft,document.body.scrollTop只用于IE6以前的版本,在IE6中,對(duì)沒(méi)有宣告 DOCTYPE,或者宣告的是transitional DOCTYPE,那么IE6將使用document.documentElement.scrollLeft 來(lái)獲取鼠標(biāo)的絕對(duì)位置。

Stephen Chapman提供的函數(shù)做個(gè)記錄

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;
}
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)用方法:
復(fù)制代碼 代碼如下:

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;
}

下面是我自己開(kāi)發(fā)項(xiàng)目中的實(shí)例:
復(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)文章

  • JavaScript判斷用戶(hù)是否對(duì)表單進(jìn)行了修改的方法

    JavaScript判斷用戶(hù)是否對(duì)表單進(jìn)行了修改的方法

    這篇文章主要介紹了JavaScript判斷用戶(hù)是否對(duì)表單進(jìn)行了修改的方法,實(shí)例分析了javascript對(duì)表單操作與判定的技巧,需要的朋友可以參考下
    2015-03-03
  • three.js利用卷積法如何實(shí)現(xiàn)物體描邊效果

    three.js利用卷積法如何實(shí)現(xiàn)物體描邊效果

    這篇文章主要給大家介紹了關(guān)于three.js利用卷積法如何實(shí)現(xiàn)物體描邊效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用three.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • JS使用Chrome瀏覽器實(shí)現(xiàn)調(diào)試線上代碼

    JS使用Chrome瀏覽器實(shí)現(xiàn)調(diào)試線上代碼

    這篇文章主要介紹了JS使用Chrome瀏覽器實(shí)現(xiàn)調(diào)試線上代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SSM+layUI 根據(jù)登錄信息顯示不同的頁(yè)面方法

    SSM+layUI 根據(jù)登錄信息顯示不同的頁(yè)面方法

    今天小編就為大家分享一篇SSM+layUI 根據(jù)登錄信息顯示不同的頁(yè)面方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09
  • js如何引入wasm文件

    js如何引入wasm文件

    這篇文章主要介紹了js如何引入wasm文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • js學(xué)使用setTimeout實(shí)現(xiàn)輪循動(dòng)畫(huà)

    js學(xué)使用setTimeout實(shí)現(xiàn)輪循動(dòng)畫(huà)

    這篇文章主要為大家詳細(xì)介紹了js使用setTimeout實(shí)現(xiàn)輪循動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • redux中間件之redux-thunk的具體使用

    redux中間件之redux-thunk的具體使用

    本篇文章主要介紹了redux中間件之redux-thunk的具體使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 微信實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)到用其他瀏覽器打開(kāi)指定APP下載

    微信實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)到用其他瀏覽器打開(kāi)指定APP下載

    這篇文章主要介紹了微信實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)到用其他瀏覽器打開(kāi)指定APP下載,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • js html5 css俄羅斯方塊游戲再現(xiàn)

    js html5 css俄羅斯方塊游戲再現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了js html5 css俄羅斯方塊游戲?qū)崿F(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • javascript函數(shù)報(bào)Uncaught?ReferenceError:?XXX?is?not?defined

    javascript函數(shù)報(bào)Uncaught?ReferenceError:?XXX?is?not?define

    本文主要介紹了javascript函數(shù)報(bào)Uncaught?ReferenceError:?XXX?is?not?defined,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論

班戈县| 峨山| 道真| 宁晋县| 昌图县| 中西区| 左贡县| 高青县| 米林县| 扶余县| 如东县| 华阴市| 宁远县| 巴林右旗| 逊克县| 长汀县| 淮阳县| 安丘市| 龙井市| 乐都县| 汶川县| 朝阳县| 阳信县| 辉县市| 册亨县| 峨眉山市| 望江县| 浮梁县| 天祝| 鸡泽县| 定南县| 望江县| 南阳市| 菏泽市| 兰西县| 崇礼县| 娄烦县| 江华| 宜兰县| 化州市| 揭西县|