昨天写的脚本在获取鼠标位置的时候有些问题。在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>
相关推荐:
ChatGPT免费版每天提问有次数限制吗?揭秘如何高效使用AI助手!,机甲ai手绘
URL泄露:如何防止信息泄露带来的严重后果,抖音推广营销服务多少钱
ChatGPT在处理文本时可能无法完全理解上下文的复杂性,肌肉ai
GPT-3.5网页版:让你与人工智能零距离接触,ai英语学
AI缩短短文-提升创作效率,写作新体验,光速写作业ai写作app
求一个AI软件,彻底改变你的工作与生活!
ChatGPT网页版内容显示不全的原因与解决方案,ai超级绘
seo给你什么帮助,seo的利与弊 ,ai 大厨
怎么用AI写出令人惊叹的文章?轻松搞定写作难题!
互联网资源的无限潜力:如何利用数字世界为个人和企业创造价值,信阳网站建设正规公司
什么是seo优化营销,seo主要优化什么 ,ai绘画国风古韵
用AI写科普文章:科技改变写作的未来
ChatGPT不能用了?了解这一背后的真相及解决方法,ai恐怖头像
什么是SEO金融,seo是做什么的 ,ai中转程序
ChatGPT:人工智能对话新时代的领航者,ai音响华为还是小米好
SEO网:让你的数字营销更加精准高效,开启网络引流新时代,丰县互联网网站推广优势
SEO字:如何通过精准关键词提升网站流量与排名,赣州于都网站推广
SEO优化工具优势:提升网站排名,带来流量和转化的秘密武器,ai圆形造字
“标题制造机”:颠覆内容创作的秘密武器,助你轻松打造吸引力十足的标题,景区线上推广用哪些网站
优化平台:让数字化转型更简单、更高效,莆田谷歌seo品牌排行
实用AI工具:提升效率、优化生活的科技利器
ChatGPT页面空白不乏登录:让你秒变职场高手与生活智囊,百度ai 腾讯ai
SEO提供:如何通过精准的SEO策略提升网站流量与品牌影响力,刷关键词排名立的火星
SEO代做:让你的企业轻松登顶搜索引擎,快速提升曝光率,seo 提高注册量
AI助手推进:智能化时代的企业革命,ai改变图片颜色
SEO全站优化:打造强大网站排名的必备利器,AI论文写作的优点
SEO更多-让你的企业站点在搜索引擎中脱颖而出,如何结交seo大神
ChatGPT画布打不开?如何解决这一常见问题?,Ai怎么储存为Ai格式在桌面
智能AI写作生成:如何借助人工智能提升创作效率与质量
chatai写作免费一键生成,轻松解决写作难题!,陈逗逗ai换脸在线看
代哥SEO-让您的网站迅速登顶搜索引擎的秘密武器,济南关键词的排名优化
SEO优化需要花钱吗?从零起步,如何让SEO成为企业的“隐形财富”,字体如何往ai里面倒
OpenAI新产品与现有技术的完美结合:赋能未来的智能变革,培训 ai
什么是seo反连接,网站反链多好还是少好 ,网易ai产品
怎么让AI润色文章,让写作更轻松?
seo网站是什么东西,seo网站是什么东西啊 ,ai锯齿消失
seo需要了解什么,seo需要学些什么内容 ,ovo ai
ChatGPT当前不可用?背后的原因与解决方案全解析,ai propos
seo网站编辑是做什么,seo网站编辑可在家兼职 ,ai慢直播
ChatGPT中文版下载免费版:智能对话新时代,尽在,ai光波
ChatGPT网页版为什么不能用了?解析原因与解决办法,女人莫名其妙想ai
SEO快速优化排名:助你网站跃升搜索引擎首页,ai彩虹立体9怎么做
seo简报什么意思,seo工作汇报 ,万花筒 ai
SEO热词:提升网站排名的关键秘诀,一句话营销推广怎么写好
SEO售产-数字营销新战场,助力企业实现盈利增长,去哪找自贡网站建设
AI提取文章重要内容:让信息抓取更高效、更精准,松鼠ai 教学
怎么用AI写文章:高效创作的秘诀
AI代写文章:高效创作的新风尚
SEO永远,数字营销的核心力量,广州seo搜索栏内容
ChatGPT免费版下载:智能对话助手带来的全新体验,电脑怎么下载Ai微认证