您的位置:首页 > 运维架构

FireFox下设置DIV的top和left无效解决方法

2010-12-31 11:40 525 查看
火狐浏览器和IE浏览器有很多冲突,往往在IE下正常的在火狐下就不正常了,这就需要程序去判断,搞定他们兼容问题!
下面这段代码解决了在火狐下div设置top和left无效的BUG和在IE下滚动条无效的BUG
------------------------------------------------JS-------------------------------------------------
function ShowNew(ev)
{
//兼容火狐和IE获取event对象
var ev = ev||window.event;

var x=0;var y=0;

if(ev.pageX || ev.pageY)
{
x=ev.pageX;y=ev.pageY;
}
else
{
//兼容火狐和IE获取滚动条的滚动参数
if (document.documentElement && document.documentElement.scrollTop)
{
t = document.documentElement.scrollTop;
l = document.documentElement.scrollLeft;
}
else if (document.body)
{
t = document.body.scrollTop;
l = document.body.scrollLeft;
}
x=(ev.clientX + l - document.body.clientLeft);y=(ev.clientY + t - document.body.clientTop);
}

alert('x:'+x+' y:'+y);//测试显示

}
----------------------------------------------HTML----------------------------------------------
<div id="NewInfo" style="z-index:100;position:absolute;">aaaaaaaa</div>
<img onmouseover="javascript:ShowNew(event);" src="a.jpg"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: