天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

通過JS 獲取Mouse Position(鼠標坐標)的代碼

昨天寫的腳本在獲取鼠標位置的時候有些問題。在IE中始終當有滾動條的時候,發現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提供的函數做個記錄

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.
還要其他的情況:
調用方法:
復制代碼 代碼如下:
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;
}

下面是我自己開發項目中的實例:
復制代碼 代碼如下:
<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>

JavaScript技術通過JS 獲取Mouse Position(鼠標坐標)的代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 免费韩国伦理2017最新 | 视频一区国产 | 99re热有精品国产 | 快播萝莉影院 | 成片在线看一区二区草莓 | 99RE8国产这里只有精品 | 秋霞电影院兔费理论观频84mb | 亚洲精品国产高清不卡在线 | 99视频精品全部免费观看 | 99re2.久久热最新地址 | 男女交性视频无遮挡全过程 | 国语自产拍在线视频普通话 | 棉签和冰块怎么弄出牛奶视频 | 久久久久久久久a免费 | 久久精品热老司机 | 影音先锋av天堂 | 粗好大用力好深快点漫画 | 被强J高H纯肉公交车啊 | 野花视频在线观看免费 | 中国农民真实bbwbbw | 久久久久99精品成人片三人毛片 | 啊灬啊别停灬用力啊老师 | 国产午夜精品理论片影院 | 四虎国产精品高清在线观看 | 久久综合九色综合国产 | 欧美一区二区三区激情视频 | 亚洲中文字幕乱码熟女在线 | 精品欧美一区二区三区四区 | 91交换论坛 | AV精品爆乳纯肉H漫网站 | 羞羞漫画免费漫画页面在线看漫画秋蝉 | 99九九99九九九视频精品 | 亚洲精品123区 | 三叶草未满十八岁 | 久久国产精品自线拍免费 | 亚洲精品久久久午夜麻豆 | 九九在线精品视频 | 国产国语在线播放视频 | 边做边爱免费视频播放 | 中国国产不卡视频在线观看 | 成年人免费观看的视频 |