五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > 通過JS 獲取Mouse Position

通過JS 獲取Mouse Position

文章來源:365jz.com     點(diǎn)擊數(shù):385    更新時(shí)間:2009-09-21 11:05   參與評(píng)論

最近我發(fā)現(xiàn)在webpage中獲取空間的絕對(duì)坐標(biāo)時(shí),如果有滾動(dòng)條就會(huì)有錯(cuò),后來用無名發(fā)現(xiàn)的方法得以解決

特此記錄

無名 的博客中轉(zhuǎn)載:

http://www.cnblogs.com/wuming/archive/2009/05/07/1452029.html

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

from :http://jnote.cn/blog/javascript/javascript-mouse-cursor-position.html

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.

還要其他的情況:

javascript 獲取控件的絕對(duì)位置

調(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;

}

如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (385人查看0條評(píng)論)
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)